文件上传是项目开发中最常见的功能之一 ,
Spring 可以很好的支持文件上传,但是 Spring 的默认环境中没有装配 MultipartResolver,
因此默认情况下其不能处理文件上传工作。
如果想使用 Spring 的文件上传功能,则需要在 Spring 环境中配置 MultipartResolver。
为了能上传文件,
必须将表单的 method 属性设置为 POST,
将 enctype 属性 设置为 multipart/form-data 。
只有在这样的情况下,浏览器才会把用户选择的文件以二进制流的形式发送给服务器。
这里不对 enctype 属性作过多的讲解,详细的可以去看 HTML enctype 属性 这篇文章。
在2003年,Apache Software Foundation发布了开源的 Commons FileUpload 组件,
其很快成为Servlet/JSP程序员上传文件的最佳选择。
SpringMVC的文件上传还需要依赖Apache Commons FileUpload的组件。
导入文件上传的jar包,commons-fileupload , Maven会自动帮我们导入他的依赖包 commons-io包;
commons-fileupload
commons-fileupload
1.3.3
配置bean:multipartResolver
注意!!!这个bena的id必须为:multipartResolver , 否则上传文件会报400的错误!
CommonsMultipartFile 的 常用方法:
前端页面
Controller
@Controller
public class FileController {
@Autowired
ServletContext servletContext;
//@RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象
//批量上传CommonsMultipartFile则为数组即可
@RequestMapping("/upload")
public String fileUpload(@RequestParam("file") CommonsMultipartFile file) throws IOException {
//获取文件名
String uploadFileName = file.getOriginalFilename();
//如果文件名为空,直接回到首页!
if ("".equals(uploadFileName)) {
return "redirect:/";
}
System.out.println("上传文件名 : " + uploadFileName);
//获取真实物理路径
String path = servletContext.getRealPath("/upload");
//如果路径不存在,创建一个
File realPath = new File(path);
if (!realPath.exists()) {
realPath.mkdir();
}
System.out.println("上传文件保存地址:" + realPath);
InputStream is = file.getInputStream(); //文件输入流
OutputStream os = new FileOutputStream(new File(realPath, uploadFileName)); //文件输出流
//读取写出
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.close();
is.close();
return "redirect:/";
}
/**
* 采用file.transferTo 来保存上传的文件
*/
@RequestMapping("/upload2")
public String fileUpload2(@RequestParam("file") CommonsMultipartFile file) throws IOException {
//获取真实物理路径
String path = servletContext.getRealPath("/upload");
File realPath = new File(path);
if (!realPath.exists()) {
realPath.mkdir();
}
//上传文件地址
System.out.println("上传文件保存地址:" + realPath);
//通过CommonsMultipartFile的方法直接写文件(注意这个时候)
file.transferTo(new File(realPath + "/" + file.getOriginalFilename()));
return "redirect:/";
}
}
注意!!!CommonsMultipartFile 为特殊参数,必须使用 @RequestParam 注解 指定前端参数,否则不能进行数据绑定。
文件下载步骤:
前端页面
Controller
@RequestMapping("/download")
public void downloads(HttpServletResponse response) throws Exception {
//要下载的资源地址
String path = servletContext.getRealPath("/upload");
String fileName = "计算机结构框图.jpg";
//1、设置response 响应头
response.reset(); //设置页面不缓存,清空buffer
response.setCharacterEncoding("UTF-8"); //字符编码
response.setContentType("multipart/form-data"); //多扩展类型表单数据
//设置响应头为 附件
response.setHeader("Content-Disposition",
"attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
File file = new File(path, fileName);
//2、 读取文件--输入流
InputStream input = new FileInputStream(file);
//3、 写出文件--输出流
OutputStream out = response.getOutputStream();
byte[] buff = new byte[1024];
int index = 0;
//4、执行 写出操作
while ((index = input.read(buff)) != -1) {
out.write(buff, 0, index);
}
out.close();
input.close();
}
以上文件上传和下载都已完成,大家可以和JavaWeb原生的方式对比一下,就可以知道这个便捷多了!
参与评论
手机查看
返回顶部