标签:
springMVC默认的解析器里面是没有加入对文件上传的解析的,使用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());
-
-
- if (multipartResolver.isMultipart(request)) {
- MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
-
- Iterator<String> iter = multiRequest.getFileNames();
- while (iter.hasNext()) {
-
-
- 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提供了更加灵活的方法,可以获取多个文件和文件名,可以遍历获得每个文件.
SpringMvc 文件上传
标签:
原文地址:http://www.cnblogs.com/thinkpad/p/4888546.html