@Override
public FileOutput uploadPicture(MultipartFile multipartFile, Integer type) throws IOException {
InputStream inputStream = multipartFile.getInputStream();
byte[] imageBytes = FileUtils.readInputStream(inputStream);
String imageType = FileUtils.getPicType(imageBytes);
String url = this.buildUploadUrl(type);
Map<String,Object> map = new HashMap<String,Object>(){{
put("file", multipartFile);
}};
String rs = null;
File updateFile = null;
if (0 == type && !"TYPE_JPG".equals(imageType)){
log.info("正在修改图片格式为JPG");
updateFile = FileUtils.modifyImageFormat(imageBytes, fileConfigProperties.getTempFilePath() + "modifyImageType.jpg", "jpg");
map.put("file", updateFile);
}
if (0 == type && (updateFile == null) ? FileUtils.checkImagePixel(multipartFile) : FileUtils.checkImagePixel(updateFile)){
log.info("正在进行图片压缩");
//压缩图片指定图片像素点
File targetFile = new File(fileConfigProperties.getTempFilePath() + "scaleAfterImage.jpg");
FileUtils.imageScale(multipartFile, targetFile);
map.put("file", targetFile);
log.info("压缩后图片大小 -> {}", FileUtils.bytes2kb(targetFile.length()));
BufferedImage bufferedImage = FileUtils.file2ImageBuffer(targetFile);
log.info("压缩后图片像素, width -> {}, height -> {}", bufferedImage.getWidth(), bufferedImage.getHeight());
rs = HttpUtil.postForStringWithFile2(url,map);
}else{
log.info("原图上传oss");
Object file = map.get("file");
if (file instanceof MultipartFile){
rs = HttpUtil.postForStringWithFile(url,map);
}
if (file instanceof File){
rs = HttpUtil.postForStringWithFile2(url,map);
}
}
log.info("上传图片返回数据:{}",rs);
// 异步删除临时文件
//this.scheduledThreadPool.poolExecutor.schedule(() -> FileUtils.deleteFile(targetFile), 100, TimeUnit.MILLISECONDS);
return this.buildResponse(rs);
}
判断图片格式/修改图片格式/压缩图片
最新推荐文章于 2022-10-20 16:38:31 发布