前情提要
现在大多数的web开发基本都会用到文件上传这一个功能,文件上传分为单文件上传和多文件上传,下面就一一讲解一下通过SpringBoot框架对两种上传的实现
SpringBoot实现单文件上传
首先建立一个html界面,包含一个form文件上传表单,代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<title>文件上传界面</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/upload">
选择文件:<input type="file" name="files" />
<br />
<button type="submit">上传</button>
<span> </span>
<button type="reset">重选</button>
</form>
</body>
</html>
然后编写/pload的请求处理控制类,实现代码如下,在代码中进行详细注释:
package org.framework.demo.section1;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.MultiPixelPackedSampleModel;
import java.io.*;
import java.util.List;
@Controller
public class FileUploadController {
@RequestMapping("/upload")
@ResponseBody
public String upload(HttpServletResponse resp,
@RequestParam("file")MultipartFile file) throws IOException {
resp.setCharacterEncoding("utf-8");
if(file.isEmpty()){
return "file is null文件为空";
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
new File(file.getOriginalFilename())));
bos.write(file.getBytes());
bos.flush();
bos.close();
return "文件上传成功 file upload success";
}
}
然后编写启动类,启动类的代码很简单,仅仅一个SpringApplication.run即可,这里就不贴出来了,自己进行测试即可
SpringBoot实现多文件上传
多文件上传相比单文件上传来说要麻烦一点,但是其实原理也就是进行遍历多个单文件上传,其处理方法代码如下:
package org.framework.demo.section1;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.MultiPixelPackedSampleModel;
import java.io.*;
import java.util.List;
@Controller
public class FileUploadController {
@RequestMapping("/uploads")
@ResponseBody
public String uploads(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setCharacterEncoding("utf-8");
List<MultipartFile> files = ((MultipartHttpServletRequest)req).getFiles("files");
if(files.size() < 1)
return "没选中任何上传文件";
for(MultipartFile file: files){
byte[] bytes = file.getBytes();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(
new File(file.getOriginalFilename())));
out.write(bytes);
out.flush();
out.close();
}
return "文件批量上传成功";
}
}
文件上传的一些属性配置
文件默认上传的大小上限是10MB,也可以通过启动类对文件上传的一些属性进行相应的自定义配置,代码如下:
/**
* 进行文件上传的相应配置
* @return
*/
@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory factory = new MultipartConfigFactory();
//设置上传的文件大小上限,如果超出限制,就会抛出异常信息
factory.setMaxFileSize("128KB");
//设置一次总上传数据的大小,用于多文件上传设置
factory.setMaxRequestSize("256KB");
return factory.createMultipartConfig();
}