SpringBoot通过commons-fileupload实现文件上传+上传进度+返回资源url

引入maven资源

<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
	<groupId>commons-fileupload</groupId>
	<artifactId>commons-fileupload</artifactId>
	<version>1.4</version>
</dependency>

<!-- hutool-all -->
<dependency>
	<groupId>cn.hutool</groupId>
	<artifactId>hutool-all</artifactId>
	<version>5.3.9</version>
</dependency>

SpringBoot配置

修改为自己需要的大小

#文件上传大小配置
spring:
  servlet:
    multipart:
      max-file-size: 1024MB
      max-request-size: 1024MB
#文件上传根目录
file:
  path: /Users/tianpuyang/uploadPath/file/

FileUploadController

uploadFile:文件上传接口

支持多文件上传,url会以逗号分割的形式返回

uploadStatus:上传进度获取接口

通过前端循环调用获取上传进度

deleteFile:文件删除接口

传入uploadFile接口生成的url即可删除

@RestController
public class FileUploadController {
   

    private final Log log = LogFactory.get();
    /**
     * 配置的上传路径
     */
    @Value("${file.path}")
    private String filePath;

    /**
     * 普通文件上传
     *
     * @param files
     * @param request
     * @return
     */
    @PostMapping("/uploadFile")
    @ApiOperation("普通文件上传")
    public JsonResult uploadFile(@RequestParam("files") List<CommonsMultipartFile> files, HttpServletRequest request) {
   
        StringBuffer buffer = new StringBuffer();
        for (CommonsMultipartFile file : files) {
   
            //获取原文件名称和后缀
            String originalFilename = file.getOriginalFilename();
            // 获取文件后缀名
            String fil_extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

            LocalDateTime now = LocalDateTimeUtil.now();
            int year = now.getYear();
            int month = now.getMonthValue();
            int day = now.getDayOfMonth();
            long milli = now.toInstant(Zone
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值