文件上传是一个最基本的功能,往往我们需要对图片进行压缩,来加快移动端的加载速度。
SprimgMVC图片上传可以参考SpringMVC传值
从这里开始
System.out.println("文件大小: " + file.getSize())
System.out.println("文件类型: " + file.getContentType())
System.out.println("表单名称: " + file.getName())
System.out.println("文件原名: " + file.getOriginalFilename())
if (!file.getContentType().contains("image")) {
return BaseReturn.response(ErrorCode.FAILURE, "不支持的图片类型:" + file.getContentType())
}
String image = ImageService.saveImage(request, file, uploadPath)
相关依赖
<!-- 图片压缩 -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
<!-- cmyk格式图片转换 -->
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tiff</artifactId>
<version>3.3</version>
</dependency>
ImageService
package com.jrbac.service
import java.awt.image.BufferedImage
import java.io.File
import java.io.IOException
import javax.imageio.ImageIO
import javax.imageio.stream.ImageOutputStream
import javax.servlet.http.HttpServletRequest
import org.apache.commons.io.FileUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.multipart.MultipartFile
import com.jrbac.util.UUIDGenerator
import net.coobird.thumbnailator.Thumbnails
public class ImageService {
private static final Logger logger = LoggerFactory.getLogger(ImageService.class)
public static String saveImage(HttpServletRequest request, MultipartFile file, String uploadPath) {
// 如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\uploadPath\\文件夹中
// String fileName = file.getOriginalFilename()
// String fileExt[] = fileName.split("\\.")
String ext = file.getContentType().split("\\/")[1]
String newFileName = UUIDGenerator.getUUID() + "." + ext
String realPath = request.getSession().getServletContext().getRealPath(uploadPath)
String filePathAndName = null
if (realPath.endsWith(File.separator)) {
filePathAndName = realPath + newFileName
} else {
filePathAndName = realPath + File.separator + newFileName
}
logger.info("-----上传的文件:{}-----", filePathAndName)
try {
// 先把文件保存到本地
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, newFileName))
} catch (IOException e1) {
logger.error("-----文件保存到本地发生异常:{}-----", e1.getMessage())
}
int big = 2 * 1024 * 1024
if (file.getSize() > big) {
thumbnail(filePathAndName, 0.6f)
} else {
thumbnail(filePathAndName, 0.8f)
}
return uploadPath + newFileName
}
private static void thumbnail(String filePathAndName, double size) {
try {
Thumbnails.of(filePathAndName).scale(size).toFile(filePathAndName)
} catch (IOException e) {
logger.error("-----读取图片发生异常:{}-----", e.getMessage())
logger.info("-----尝试cmyk转化-----")
File cmykJPEGFile = new File(filePathAndName)
try {
BufferedImage image = ImageIO.read(cmykJPEGFile)
ImageOutputStream output = ImageIO.createImageOutputStream(cmykJPEGFile)
if (!ImageIO.write(image, "jpg", output)) {
logger.info("-----cmyk转化异常:{}-----")
}
Thumbnails.of(image).scale(0.4f).toFile(filePathAndName)
logger.info("-----cmyk转化成功-----")
} catch (IOException e1) {
logger.info("-----cmyk转化异常:{}-----", e1.getMessage())
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
这里有一点需要解释一下
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, newFileName));
会将图片保存到本地,
这一步没问题
问题会发生在
Thumbnails.of(filePathAndName).scale(size).toFile(filePathAndName);
大部分情况下是不会出问题的,如果
P过的图片保存为jpg格式时,默认的模式是CMYK模式
就会报如下错误
javax.imageio.IIOException: Unsupported Image Type
这里采用https://github.com/haraldk/TwelveMonkeys工具解决