public static MultipartFile zipFile(MultipartFile file) throws IOException {
InputStream in = file.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Thumbnails.of(in).scale(0.5f).outputQuality(0.65f).toOutputStream(out);
in = new ByteArrayInputStream(out.toByteArray());
return getMultipartFile(in,file.getOriginalFilename());
}
public static MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
FileItem fileItem = createFileItem(inputStream, fileName);
return new CommonsMultipartFile(fileItem);
}
public static FileItem createFileItem(InputStream inputStream, String fileName) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
String textFieldName = "file";
FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
OutputStream os = null;
try {
os = item.getOutputStream();
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
inputStream.close();
} catch (IOException e) {
throw new IllegalArgumentException("文件上传失败");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
return item;
}