1、代碼上傳圖片成功,瀏覽器直接訪問下載問題:
(1)使用三級域名;
(2)指定上傳文件的Content-Type(OSS jar包版本可能不一致,對號入座):
ObjectMetadata objectMeta = new ObjectMetadata();
objectMeta.setContentType("image/jpg");//在metadata中標記文件類型
objectMeta.setContentLength(out.toByteArray().length);
若在阿里雲中設置,則點擊文件右鍵設置HTTP頭即可:
2、關於圖片上傳的顯示:
圖片路徑+?x-oss-process=image/resize,m_fill,h_100,w_100 //可指定訪問壓縮尺寸的圖片
圖片路徑+?x-oss-process=image/quality,q_20 //按像素壓縮訪問的圖片
上述這些,可以用來對於已壓縮上傳圖片,需要在app中顯示時進一步調整。
不過,阿里雲文檔並沒有提供壓縮上傳的功能,只針對在雲上的圖片顯示做了很多豐富的處理。
3、圖片上傳壓縮(參考一些上傳,可以使用的)
第一次使用的是Thumbnailator,但不知道爲什麼不管怎麼修改outputQuality(0.25f)值,雖然壓縮了,但設置0.2和0.3效果並沒有區別大小都沒有變。沒有試過壓縮尺寸,這個不行也就沒有再試了。
(1)原尺寸不變減少分辨率(對於大圖片並不提倡,因爲在手機上不忍直視,試過5M的,雖然壓縮的可以,但失真嚴重,下面的方法對於png圖片不可以,需要轉換,暫時沒有解決辦法
https://stackoverflow.com/questions/15318677/how-do-i-write-a-bufferedimage-as-a-png-with-no-compression )
public class Test {
public static void main(String[] args) throws IOException {
String endpoint = "";
String accessKeyId = "";
String accessKeySecret = "";
String bucketName = "";//實際
//key
String key = "images/23_iso100_14mm6.jpg";
InputStream fileStream = new FileInputStream("D:/23_iso100_14mm.jpg");
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[10485760]; //暫時上限10M
int rc = 0;
while ((rc = fileStream.read(buff, 0, 10485760)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] tempByte = compressPicByQuality(swapStream.toByteArray(), 0.2f);
InputStream sbs = new ByteArrayInputStream(tempByte);
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
ObjectMetadata objectMeta = new ObjectMetadata();
objectMeta.setContentType("image/jpg");//在metadata中標記文件類型
objectMeta.setContentLength(tempByte.length);
ossClient.putObject(bucketName, key, sbs,objectMeta);
}
/**
* @Title: compressPicByQuality @Description: 壓縮圖片,通過壓縮圖片質量,保持原圖大小 @param quality:0-1 @return byte[] @throws
*/
public static byte[] compressPicByQuality(byte[] imgByte, float quality) {
byte[] inByte = null;
try {
ByteArrayInputStream byteInput = new ByteArrayInputStream(imgByte);
BufferedImage image = ImageIO.read(byteInput);
// 如果圖片空,返回空
if (image == null) {
return null;
}
// 得到指定Format圖片的writer
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpg");// 得到迭代器,jpg,jpeg
ImageWriter writer = (ImageWriter) iter.next(); // 得到writer
// 得到指定writer的輸出參數設置(ImageWriteParam )
//ImageWriteParam iwp = writer.getDefaultWriteParam();
ImageWriteParam iwp = new JPEGImageWriteParam(null);
//ImageWriteParam iwp = new P
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // 設置可否壓縮
iwp.setCompressionQuality(quality); // 設置壓縮質量參數
iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
ColorModel colorModel = ColorModel.getRGBdefault();
// 指定壓縮時使用的色彩模式
iwp.setDestinationType(
new javax.imageio.ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16)));
// 開始打包圖片,寫入byte[]
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 取得內存輸出流
IIOImage iIamge = new IIOImage(image, null, null);
// 此處因爲ImageWriter中用來接收write信息的output要求必須是ImageOutput
// 通過ImageIo中的靜態方法,得到byteArrayOutputStream的ImageOutput
writer.setOutput(ImageIO.createImageOutputStream(byteArrayOutputStream));
writer.write(null, iIamge, iwp);
inByte = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
System.out.println("write errro");
e.printStackTrace();
}
return inByte;
}
}
(2)改變尺寸壓縮(比較好些,暫時使用,不過對於png圖片來說,壓縮困難,有專門的國外付費軟件,暫不考慮,下面會有jpg和png壓縮後的比較,清晰度都還行)
public class Test {
public static void main(String[] args) throws IOException {
String endpoint = "";
String accessKeyId = "";
String accessKeySecret = "";
String bucketName = "";
//key
String key = "23_iso100_14mm6.jpg";
InputStream fileStream = new FileInputStream("D:/23_iso100_14mm.jpg");
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[10485760]; //暫時上限10M
int rc = 0;
while ((rc = fileStream.read(buff, 0, 10485760)) > 0) {
swapStream.write(buff, 0, rc);
}
ByteArrayInputStream in = new ByteArrayInputStream(swapStream.toByteArray()); //將b作爲輸入流;
BufferedImage image = ImageIO.read(in);
//1500 1500 jpg 268kb png 2.27M
//1000 1000 jpg 125kb png 1.27M(還是比較大的)
ArrayList<Integer> arrayList = getAutoWidthAndHeight(image,1000,1000);
int w = arrayList.get(0);
int h = arrayList.get(1);
Image newImage = image.getScaledInstance(w, h,Image.SCALE_DEFAULT);
BufferedImage outputImage = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);
Graphics2D g = outputImage.createGraphics();
g.drawImage(newImage, 0, 0, null); // 繪製縮小後的圖
g.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(outputImage,"jpg",out);
InputStream sbs = new ByteArrayInputStream(out.toByteArray());
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
ObjectMetadata objectMeta = new ObjectMetadata();
objectMeta.setContentType("image/jpg");//在metadata中標記文件類型
objectMeta.setContentLength(out.toByteArray().length);
ossClient.putObject(bucketName, key, sbs,objectMeta);
}
/***
*
* @param bufferedImage 要縮放的圖片對象
* @param width_scale 要縮放到的寬度
* @param height_scale 要縮放到的高度
* @return 一個集合,第一個元素爲寬度,第二個元素爲高度
*/
public ArrayList<Integer> getAutoWidthAndHeight(BufferedImage bufferedImage,int width_scale,int height_scale){
ArrayList<Integer> arrayList = new ArrayList<Integer>();
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
if(width<=1000&&height<=1000){//限制不壓縮(簡陋版。。)
arrayList.add(width);
arrayList.add(height);
}else{
double scale_w =getDot2Decimal( width_scale,width);
System.out.println("getAutoWidthAndHeight width="+width + "scale_w="+scale_w);
double scale_h = getDot2Decimal(height_scale,height);
if (scale_w<scale_h) {
arrayList.add(parseDoubleToInt(scale_w*width));
arrayList.add(parseDoubleToInt(scale_w*height));
}
else {
arrayList.add(parseDoubleToInt(scale_h*width));
arrayList.add(parseDoubleToInt(scale_h*height));
}
}
return arrayList;
}
}