1:首先在项目的pom文件中添加两个依赖
commons-fileupload
commons-fileupload
1.3.1
net.coobird
thumbnailator
0.4.8
2:resource中添加 file-message.properties 配置文件
file-message.properties.png
配置文件内容如下
#文件压缩大小(大于4兆压缩)
message.fileSize=4194304
#图片保存路径
message.upPath=D:\\MyProjectName\\UploadData\\images
#压缩比例
message.scaleRatio=0.20f
#图片类型
message.imageType=png,jpg,jpeg
3:新建 MessageProperties 类,对应 file-message.properties 配置文件
@Component
@ConfigurationProperties(prefix="message")
@PropertySource("classpath:file-message.properties")
public class MessageProperties {
private long fileSize; //压缩大小
private double scaleRatio; //压缩比例
private String upPath; //保存路径
private String imageType; //图片类型
public long getFileSize() {
return fileSize;
}
public void setFileSize(long fileSize) {
this.fileSize = fileSize;
}
public double getScaleRatio() {
return scaleRatio;
}
public void setScaleRatio(double scaleRatio) {
this.scaleRatio = scaleRatio;
}
public String getUpPath() {
return upPath;
}
public void setUpPath(String upPath) {
this.upPath = upPath;
}
public String getImageType() {
return imageType;
}
public void setImageType(String imageType) {
this.imageType = imageType;
}
}
当然也可以不新建 file-message.properties 文件, file-message.properties 文件里的内容可以直接写在 application.properties 配置文件中,那MessageProperties 类就应该按下面这种写法:
@Component
public class MessageProperties {
@Value("${fileSize}")
private long fileSize; //压缩大小
@Value("${scaleRatio}")
private double scaleRatio; //压缩比例
@Value("${MDDIMG_LOCATION}")
private String upPath; //保存路径
@Value("${imageType}")
private String imageType; //图片类型
...
}
4:service层接口
public interface FileUpAndDownService {
Map uploadPicture(MultipartFile file) throws ServiceException;
}
5:service层接口实现
@Service
public class FileUpAndDownServiceImp