• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

java springBoot上传文件/文件夹使用

武飞扬头像
乐七_
帮助2

最近项目中遇到一个文件批量上传的需求,对单个的文件、多文件或者文件夹的方式上传文件都可以满足要求,总结一下使用经验。
案例基于springBoot.
1、文件上传请求
这里postman测试了单文件和多文件的上传,同时测试了文件夹方式上传。
postman中可以选择单文件或者Ctrl键选择多文件上传。如下图:
学新通
简单html实现文件夹方式的文件上传示例:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文件上传</title>
</head>
<body>
    <h1>文件上传</h1>
    <form action="http://localhost:8888/hello" enctype="multipart/form-data" method="post">
        <input type="hidden" name="type" value="1"/>
        <input type="hidden" name="readFileType" value="jpg"/>
        <input id="dir" type="file" name="file" webkitdirectory mozdirectory/>
        <input id="uploadDir" type="submit" value="提交文件夹">
    </form>
</body>
</html>

2、编写配置类,springboot默认使用的是StandardServletMultipartResolver来处理Multipart,对应的使用StandardMultipartFile来接收文件数据,但是StandardMultipartFile获取的文件名如果是中文的话,总是乱码。
这里使用CommonMultiPart。

@Configuration
public class MultipartConfiguration {

    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver getCommonsMultipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(524288000);
        multipartResolver.setMaxInMemorySize(524288000);
        return multipartResolver;
    }
}

并且在启动类上排除自动配置MultipartAutoConfiguration

@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})

3、控制层

@Api(value = "数据接入", tags = "数据导入")
@RestController
@RequestMapping("/hello 这里写自己的请求")
public class OfflineDataController {
@ApiOperation(value = "数据批量上传")
    @AopLog(title = "数据批量上传")
    @RequestMapping(value = "uploadBatch", method = RequestMethod.POST)
    public Object uploadBatch(HelloVo helloVo) {
        ResultMap resultMap = offlineDataService.uploadBatch(helloVo);
        return resultMap;
    }
}

4、数据传输对象,其实就只要把file定义为一个集合就可以了,这样上传的所有文件都会封装到这里。

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("com.sjg.HelloVo")
public class HelloVo implements Serializable {

    @ApiModelProperty("导入多个文件")
    private List<MultipartFile> file;

    @ApiModelProperty("read文件类型")
    private String readFileType;

    @ApiModelProperty("数据相关性")
    private String relation;
}

5、服务层实现类
不需要对文件上传的绝对路径进行递归处理,直接就获取文件名称,使用上传等处理即可。干就完了!

@Service
public class OfflineDataServiceImpl implements OfflineDataService {
	@Override
    public ResultMap uploadBatch(HelloVo helloVo) {
		//获取所有文件
        List<MultipartFile> files = helloVo.getFile();
        //中间的其他处理过程
        for (MultipartFile file : files) {
                String name = "";
                //取得当前上传文件的文件名称
                name = file.getOriginalFilename();
                String fileName = name == null ? "" : name.split("\\.")[0];
                //校验上传文件后缀是否与所选择的文件类型相同
                String fileSuffix = name == null ? "" : name.split("\\.")[1];
        }
        //。。。其他处理并返回
        return ResultMap.ok("导入成功!");
	}
}

可能的问题:上传文件有大小限制,在application.yml中配置

spring: 
  servlet: 
    multipart: 
      # 设置单个文件的大小
      max-file-size: 100MB
      # 总上传文件大小
      max-request-size: 500MB

选择文件夹上传文件即可
学新通
不足之处,各位大佬评论指出,感谢!

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhcighef
系列文章
更多 icon
同类精品
更多 icon
继续加载