SpringMvc的Controller层文件上传路径:

@PostMapping("/upload")

public void fileUpload(HttpServletRequest request, MultipartFile[] inputFile) throws Exception {
for (MultipartFile file : inputFile) {
if (file.isEmpty()) {
System.out.println("文件未上传");
} else {
//获取上传文件名称
String originaFilename = file.getOriginalFilename();
String fileExtention = originaFilename.substring(originaFilename.lastIndexOf("."));
System.out.println("文件名称:" + file.getOriginalFilename());
Date date = new Date();
//文件存放的路径,并根据当天日期建立文件夹
String path = "E:/springboot/" + new SimpleDateFormat("yyyy/MM/dd/").format(date);
//建立文件夹
FileUtil.createFolder(path);
// 生成新的文件名,用系统当前时间+3位随机数,截取文件后缀名
String filename = IDUtils.getFileName() + fileExtention;
File destFile = new File(path + filename);
try {
//保存文件...
file.transferTo(destFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}

}

html页面:

spring boot 多文件上传

使用ajax上传文件:

$(function(){
var uploadImagePath;
//上传
$("#inputFile").change(function(){
var option={
type:'POST',
url:'/upload',
dataType:'text',
success:function(data){
}
};
//这里使用了jquery.form.js插件提交表单
$("#uploadForm").ajaxSubmit(option);
});

})

注:springboot2默认文件上传大小是2M,上传大文件会报异常。

并且在application.propertys配置参数是无效的。

需手动配置Bean:

spring boot 多文件上传







相关文章:

  • 2021-10-29
  • 2021-10-03
  • 2018-03-13
  • 2021-06-30
  • 2021-06-15
  • 2021-08-04
  • 2021-12-19
  • 2021-11-27
猜你喜欢
  • 2021-11-27
  • 2021-12-23
  • 2018-03-09
  • 2021-04-21
  • 2021-10-26
  • 2021-07-24
  • 2020-10-03
相关资源
相似解决方案