springboot多文件上传
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot多文件上传相关的知识,希望对你有一定的参考价值。
参考技术A MultipartFile提供了以下方法来获取上传文件的信息:getOriginalFilename,获取上传的文件名字;
getBytes,获取上传文件内容,转为字节数组;
getInputStream,获取一个InputStream;
isEmpty,文件上传内容为空,或者根本就没有文件上传;
getSize,文件上传的大小。
transferTo(File dest),保存文件到目标文件系统;
同时上传多个文件,则使用MultipartFile数组类来接受多个文件上传:
//多文件上传 @RequestMapping(value = "/batch/upload", method = RequestMethod.POST)
@ResponseBody public String handleFileUpload(HttpServletRequest request)
List<MultipartFile> files = ((MultipartHttpServletRequest) request)
.getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i = 0; i < files.size(); ++i)
file = files.get(i);
if (!file.isEmpty())
try
byte[] bytes = file.getBytes();
stream = new BufferedOutputStream(new FileOutputStream(
new File(file.getOriginalFilename())));
stream.write(bytes);
stream.close();
catch (Exception e)
stream = null;
return "You failed to upload " + i + " => " + e.getMessage();
else
return "You failed to upload " + i
+ " because the file was empty.";
return "upload successful";
可以通过配置application.properties对SpringBoot上传的文件进行限定默认为如下配置:
spring.servlet.multipart.enabled=true
spring.servlet.multipart.file-size-threshold=0
spring.servlet.multipart.location=
spring.servlet.multipart.max-file-size=1MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.resolve-lazily=false
enabled默认为true,既允许附件上传。
file-size-threshold限定了当上传文件超过一定长度时,就先写到临时文件里。有助于上传文件不占用过多的内存,单位是MB或KB,默认0,既不限定阈值。
location指的是临时文件的存放目录,如果不设定,则web服务器提供一个临时目录。
max-file-size属性指定了单个文件的最大长度,默认1MB,max-request-size属性说明单次HTTP请求上传的最大长度,默认10MB.
resolve-lazily表示当文件和参数被访问的时候再被解析成文件。
springboot+thymleaf处理多文件上传
1thymleaf页面表单
<form method="POST" enctype="multipart/form-data" action="/moreupload">
<p>文件1:<input type="file" name="file" /></p>
<p>文件2:<input type="file" name="file" /></p>
<p>文件3:<input type="file" name="file" /></p>
<p><input type="submit" value="上传" /></p>
</form>
2写一个controller跳转到页面
@RequestMapping(value = "/moresave")
public String moresave(){
return "index2";
}
3写一个controller处理多文件上传
@RequestMapping(value="/moreupload", method= RequestMethod.POST)
@ResponseBody
public String handleFileUpload(HttpServletRequest request){
List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i =0; i< files.size(); ++i) {
file = files.get(i);
if (!file.isEmpty()) {
try {
String contentType = file.getContentType();
String fileName = file.getOriginalFilename();
System.out.println("fileName-->" + fileName);
System.out.println("getContentType-->" + contentType);
String filePath = request.getSession().getServletContext().getRealPath("imgupload/");
System.out.println("filePath -->"+filePath );
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(filePath + fileName)));
System.out.println("bufferedOutputStream --->>>"+bufferedOutputStream );
bufferedOutputStream.write(file.getBytes());
bufferedOutputStream.flush();
bufferedOutputStream.close();
} catch (Exception e) {
return "You failed to upload " + i + " => " + e.getMessage();
}
} else {
return "You failed to upload " + i + " because the file was empty.";
}
}
return "上传成功";
}
4展示
以上是关于springboot多文件上传的主要内容,如果未能解决你的问题,请参考以下文章