SpringMVC文件上传
SpringMVC文件上传
文件上传的必要前提
- form表单的
enctype
属性(表单请求正文的类型)必须是multipart/form-data
(默认值是application/x-www-form-urlencoded
) method
属性必须是是POST
- 提供一个文件选择区域
<input type="file">
传统方式文件上传
@RequestMapping("/testFileUpload")
public String testFileUpload(HttpServletRequest request) throws Exception {
System.out.println("FileUpload...");
String path = request.getSession().getServletContext().getRealPath("/uploads/");
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
// 解析request对象,获取上传文件项
DiskFileItemFactory itemFactory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(itemFactory);
//解析request
List<FileItem> items = upload.parseRequest(request);
//遍历
for (FileItem item : items) {
// 进行判断,当前item对象是否是上传文件项
if (item.isFormField()) {
// 说明普通表单向
} else {
// 说明上传文件项
// 获取上传文件的名称
String filename = item.getName();
// 把文件的名称设置唯一值,uuid
String uuid = UUID.randomUUID().toString().replace("-", "");
filename = uuid + "_" + filename;
// 完成文件上传
item.write(new File(path, filename));
// 删除临时文件
item.delete();
}
}
return "test";
}
SpringMVC文件上传
- SpringMVC框架提供了
MultipartFile
对象,该对象表示上传的文件,要求变量名必须和表单的name属性名称相同。
- 在 springmvc.xml 中配置文件解析器,要求id名称必须是
multipartResolver
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
id="multipartResolver">
<!--指定以字节为单位的最大文件大小 10 * 1024 * 1024 = 10485760kb 即 10mb-->
<property name="maxUploadSize" value="10485760"/>
</bean>
- 代码如下
@RequestMapping("/testFileUpload2")
public String testFileUpload2(HttpServletRequest request, MultipartFile upload, Model model) throws Exception {
System.out.println("Springmvc FileUpload...");
String path = request.getSession().getServletContext().getRealPath("/uploads/");
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
// 获取上传文件的名称
String filename = upload.getOriginalFilename();
// 把文件的名称设置唯一值,uuid
String uuid = UUID.randomUUID().toString().replace("-", "");
filename = uuid + "_" + filename;
// 完成文件上传
upload.transferTo(new File(path, filename));
model.addAttribute("msg", "文件上传成功");
return "test";
}
SpringMVC跨服务器文件上传
-
搭建文件上传服务器
-
实现跨服务器文件上传
-
导入开发需要的jar包
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.19.4</version> </dependency>
-
编写文件上传的页面
<h3>SpringMVC跨服务器文件上传</h3> <form action="${pageContext.request.contextPath}/testFileUpload3" method="post" enctype="multipart/form-data"> <input type="file" name="upload"><br> <input type="submit" value="上传"><br> </form>
-
编写跨服务器上传的Controller
@RequestMapping("/testFileUpload3") public String testFileUpload3(MultipartFile upload, Model model) throws Exception { System.out.println("Springmvc跨服务器FileUpload..."); //定义上传文件服务器路径 String path = "http://localhost:9090/uploads/"; // 说明上传文件项 // 获取上传文件的名称 String filename = upload.getOriginalFilename(); // 把文件的名称设置唯一值,uuid String uuid = UUID.randomUUID().toString().replace("-", ""); filename = uuid + "_" + filename; //创建客户端对象 Client client = Client.create(); //和图片服务器进行连接 WebResource resource = client.resource(path+filename); //上传文件 resource.put(upload.getBytes()); model.addAttribute("msg", "文件上传成功"); return "test"; }
-
出现的问题:
-
409:部署的文件上传服务器没有uploads文件夹,手动添加该文件夹。
-
405:Tomcat的安装目录 conf 下的 web.xml 中
<Servlet>
标签中中添加如下配置<init-param> <param-name>readonly</param-name> <param-value>false</param-value> </init-param>
加入此行的含义是:接收文件的目标服务器可以支持写入操作。保存重新启动Tomact服务器就可以完美运行了