上传大图片时速度很慢,所以对图片进行压缩之后再进行存储
Thumbnailator官网:http://code.google.com/p/thumbnailator/
pom中添加
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
@PostMapping("/upload")
public Object upload(@RequestParam("file") MultipartFile file) throws IOException {
// if(file.getSize() > 2 * ONE_MB){
// return ResponseUtil.fail(500,"图片大小超出2M!");
// }
String originalFilename = file.getOriginalFilename();
logger.info("压缩前:"+file.getSize());
ByteArrayInputStream inputStream = uploadFile(file);
logger.info("压缩后:"+inputStream.available());
LitemallStorage litemallStorage = storageService.store(inputStream, inputStream.available(), file.getContentType(), originalFilename);
return ResponseUtil.ok(litemallStorage);
}
/**
* 压缩图片
* @return
*/
public static ByteArrayInputStream uploadFile(MultipartFile file){
if(file == null)return null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
Thumbnails.of(file.getInputStream()).scale(0.4f).outputQuality(0.25f).toOutputStream(baos);
if(baos!=null)
return parse(baos);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// outputStream转inputStream
public static ByteArrayInputStream parse(OutputStream out) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos = (ByteArrayOutputStream) out;
ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
return swapStream;
}
/**
* 存储一个文件对象
*
* @param inputStream 文件输入流
* @param contentLength 文件长度
* @param contentType 文件类型
* @param fileName 文件索引名
*/
public LitemallStorage store(InputStream inputStream, long contentLength, String contentType, String fileName) {
String key = generateKey(fileName);
store(inputStream, contentLength, contentType, key);
String url = generateUrl(key);
LitemallStorage storageInfo = new LitemallStorage();
storageInfo.setName(fileName);
storageInfo.setSize((int) contentLength);
storageInfo.setType(contentType);
storageInfo.setKey(key);
storageInfo.setUrl(url);
litemallStorageService.add(storageInfo);
return storageInfo;
}
@Override
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
try {
// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(contentLength);
objectMetadata.setContentType(contentType);
// 对象键(Key)是对象在存储桶中的唯一标识。
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata);
PutObjectResult putObjectResult = getOSSClient().putObject(putObjectRequest);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
}
//按指定大小把图片进行缩和放(会遵循原图高宽比例)
//此处把图片压成400×500的缩略图
Thumbnails.of(fromPic).size(400,500).toFile(toPic);
按照指定比例进行缩小和放大 //按照比例进行缩小和放大
Thumbnails.of(fromPic).scale(0.2f).toFile(toPic);//按比例缩小
Thumbnails.of(fromPic).scale(2f);//按比例放大 图片尺寸不变,压缩图片文件大小
//图片尺寸不变,压缩图片文件大小outputQuality实现,参数1为最高质量
Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic);