springBoot处理上传图片进行旋转
public class ImageUtil {
public static MultipartFile changeThePicture(MultipartFile file,double degree) throws IOException{
BufferedImage srcImage = ImageIO.read(file.getInputStream());
int width = srcImage.getWidth();
int height = srcImage.getHeight();
double sin = Math.abs(Math.sin(Math.toRadians(degree))),
cos = Math.abs(Math.cos(Math.toRadians(degree)));
int newWidth = (int) Math.floor(width * cos + height * sin),
newHeight = (int) Math.floor(height * cos + width * sin);
BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, srcImage.getType());
Graphics2D g2d = rotatedImage.createGraphics();
AffineTransform at = new AffineTransform();
at.translate((newWidth - width) / 2, (newHeight - height) / 2);
at.rotate(Math.toRadians(degree), width / 2, height / 2);
g2d.setTransform(at);
g2d.drawImage(srcImage, 0, 0, null);
g2d.dispose();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(rotatedImage, "png", outputStream);
byte[] imageBytes = outputStream.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);
String ex = file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf("."));
MultipartFile multipartFile = new MockMultipartFile(ex, file.getOriginalFilename(), "image/jpeg", bais);
return multipartFile;
}
}
@PostMapping("/uploadSignePicture")
@Operation(summary = "上传签名文件")
public CommonResult<Map<String, String>> uploadSignePicture(@RequestParam("file") MultipartFile file) {
try {
S3FileClient fileClient = new S3FileClient(1L, s3FileClientConfig);
fileClient.init();
file = ImageUtil.changeThePicture(file,270);
InputStream in = file.getInputStream();
byte[] content = in.readAllBytes();
String originalFilename = file.getOriginalFilename();
if ("".equals(originalFilename)) {
return CommonResult.error(500, "请选择文件上传!");
}
String path = FileUtils.generatePath(content, originalFilename);
String nowDate = String.valueOf(LocalDate.now());
path = applicationName + "/" + nowDate + "/" + path;
String type = FileTypeUtils.getMineType(content, originalFilename);
in.close();
String url = fileClient.upload(content, path, type);
Map<String, String> map = new HashMap<>();
map.put("filePath", String.format("/%s%s", applicationName, url.substring(url.lastIndexOf("/", url.lastIndexOf("/") - 10))));
map.put("fileName", originalFilename);
return success(map);
} catch (Exception e) {
log.error("minio文件上传失败", e);
return CommonResult.error(500, "文件上传失败,请稍后再试!");
}
}