文件上传的必要前提
- form 表单的 enctype 取值必须是:multipart/form-data (默认值是:application/x-www-form-urlencoded)
- method 属性取值必须是 Post
- 提供一个文件选择域
<input type=”file” />
- 导入jar包:借助第三方组件实现文件上传
- 使用 Commons-fileupload 组件实现文件上传,需要导入该组件相应的支撑 jar 包:Commons-fileupload 和 commons-io。commons-io 不属于文件上传组件的开发 jar 文件,但Commons-fileupload 组件从 1.1 版本开始,它工作时需要 commons-io 包的支持。
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency
传统文件上传
- JSP页面
<form action="File/upload" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload">
<input type="submit" value="提交">
</form>
- 编写Controller
@RequestMapping("/upload")
public String upload(HttpServletRequest request) throws Exception {
String path = request.getSession().getServletContext().getRealPath("/uploads");
System.out.println(path);
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
List<FileItem> list = sfu.parseRequest(request);
for (FileItem fileItem: list) {
if(fileItem.isFormField()){
}else {
String filename = fileItem.getName();
System.out.println(filename);
fileItem.write(new File(path, filename));
fileItem.delete();
}
}
return "success";
}
SpringMVC传统方式文件上传
- 分析原理
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200620112206859.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxODE2NTE2,size_16,color_FFFFFF,t_70)
- JSP页面
<form action="File/upload2" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload">
<input type="submit" value="提交">
</form>
- 配置文件解析器
<!-- 配置文件解析器对象,要求id名称必须是multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/>
</bean>
- 编写方法
@RequestMapping("/upload2")
public String upload2(HttpServletRequest request,MultipartFile upload) throws Exception {
String path = request.getSession().getServletContext().getRealPath("/uploads");
System.out.println(path);
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
String filename = upload.getOriginalFilename();
System.out.println(filename);
String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
filename = uuid+"_"+filename;
upload.transferTo(new File(path,filename));
return "success";
}
SpringMVC跨服务器方式文件上传
- 导入开发需要的jar包
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.1</version>
</dependency>
- 编写文件上传的JSP页面
<h3>跨服务器的文件上传</h3>
<form action="user/fileupload3" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload"/><br/>
<input type="submit" value="上传文件"/>
</form>
- 编写控制器
@RequestMapping(value="/fileupload3")
public String fileupload3(MultipartFile upload) throws Exception {
System.out.println("SpringMVC跨服务器方式的文件上传...");
String path = "http://localhost:9090/springmvc_1/uploads/";
String filename = upload.getOriginalFilename();
String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
filename = uuid+"_"+filename;
Client client = Client.create();
WebResource webResource = client.resource(path+filename);
webResource.put(upload.getBytes());
return "success";
}