springboot项目引入图片_SpringBoot实现上传图片功能

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值