MultipartResolver实现文件上传 MultipartResolver实现文件上传功能
紫_色 人气:0springMVC默认的解析器里面是没有加入对文件上传的解析的,,使用springmvc对文件上传的解析器来处理文件上传的时需要用springmvc提供的MultipartResolver的申明,又因为CommonsMultipartResolver实现了MultipartResolver接口,所以我们可以在springmvc配置文件中这样配置:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="10485760000" /> <property name="maxInMemorySize" value="40960" /> </bean>
首先引入文件上传所需要的包,commons-logging-*.jar commons-io-*.jar commons-fileupload-*.jar
新建一个JSP页面.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>文件上传</title> </head> <body> <%--<form action="user/fileUpload" method="post" enctype="multipart/form-data">--%> <form action="user/fileUpload" method="post" enctype="multipart/form-data"> <input type="file" name="fileUpload" /> <input type="submit" value="上传" /> </form> </body> </html>
springmvc上传文件的形式有很多,这里我介绍两种.
第一种,看Controller
package gd.hz.springmvc.controller; import java.io.File; import java.io.IOException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.servlet.ModelAndView; @Controller("userController") @RequestMapping("user") public class UserController { // 处理文件上传一 @RequestMapping(value = "fileUpload", method = RequestMethod.POST) public ModelAndView fileUpload( @RequestParam("fileUpload") CommonsMultipartFile file) { // 获取文件类型 System.out.println(file.getContentType()); // 获取文件大小 System.out.println(file.getSize()); // 获取文件名称 System.out.println(file.getOriginalFilename()); // 判断文件是否存在 if (!file.isEmpty()) { String path = "D:/" + file.getOriginalFilename(); File localFile = new File(path); try { file.transferTo(localFile); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return new ModelAndView("dataSuccess"); } }
类CommonsMultipartFile为我们提供了许多对文件处理的方法.例如文件大小,上传文件名称,文件类型,具体用法可以查看spring的文档.transferTo就是将文件输出到指定地方.
文件上传的第二种方法,这种方法比较常用:
package gd.hz.springmvc.controller; import java.io.File; import java.io.IOException; import java.util.Iterator; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartResolver; @Controller("userController") @RequestMapping("user") public class UserController { // 处理文件上传二 @RequestMapping(value = "fileUpload2", method = RequestMethod.POST) public String fileUpload2(HttpServletRequest request) throws IllegalStateException, IOException { // 设置上下方文 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); // 检查form是否有enctype="multipart/form-data" if (multipartResolver.isMultipart(request)) { MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator<String> iter = multiRequest.getFileNames(); while (iter.hasNext()) { // 由CommonsMultipartFile继承而来,拥有上面的方法. MultipartFile file = multiRequest.getFile(iter.next()); if (file != null) { String fileName = "demoUpload" + file.getOriginalFilename(); String path = "D:/" + fileName; File localFile = new File(path); file.transferTo(localFile); } } } return "dataSuccess"; } }
MultipartHttpServletRequest提供了更加灵活的方法,可以获取多个文件和文件名,可以遍历获得每个文件.
加载全部内容