springMVC---文件上传
直接上干货吧。先看看目录结构,没有用的我都涂了:
然后是springMVC的配置。
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 <display-name></display-name> 8 9 <servlet> 10 <servlet-name>springmvc</servlet-name> 11 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 12 <init-param> 13 <param-name>contextConfigLocation</param-name> 14 <param-value>classpath:springmvc.xml</param-value> 15 </init-param> 16 <load-on-startup>1</load-on-startup> 17 </servlet> 18 19 <!-- 访问路径配置 --> 20 <servlet-mapping> 21 <servlet-name>springmvc</servlet-name> 22 <url-pattern>*.do</url-pattern> 23 </servlet-mapping> 24 25 <welcome-file-list> 26 <welcome-file>index.jsp</welcome-file> 27 </welcome-file-list> 28 </web-app>
springmvc配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd"> 11 12 <!-- 配置视图渲染器 --> 13 <bean id="jspViewResolver" 14 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 15 <property name="viewClass" 16 value="org.springframework.web.servlet.view.JstlView" /> 17 <!-- 将视图名 渲染后视图的前缀 --> 18 <property name="prefix" value="/WEB-INF/jsp/" /> 19 <!-- 渲染后视图的后缀 --> 20 <property name="suffix" value=".jsp" /> 21 </bean> 22 23 <!-- 根据传进来的URL自动扫描com.SafeCode.controller下的命名空间 --> 24 <context:component-scan base-package="cn.springmvc.controller"/> 25 26 <!-- 文件上传 --> 27 <bean id="multipartResolver" 28 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 29 <!-- 设置上传文件的最大尺寸为5MB --> 30 <property name="maxUploadSize"> 31 <value>5242880</value> 32 </property> 33 </bean> 34 </beans>
完成文件的上传功能,最重要的是在springmvc.xml中配置的这个multipartResolver,需要导入相关jar包,还要在controller中用到MultipartFile声明对象。
controller:
1 package cn.springmvc.controller; 2 3 import java.io.File; 4 5 import javax.servlet.http.HttpServletRequest; 6 7 import org.springframework.stereotype.Controller; 8 import org.springframework.ui.ModelMap; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RequestMethod; 11 import org.springframework.web.bind.annotation.RequestParam; 12 import org.springframework.web.multipart.MultipartFile; 13 14 @Controller 15 public class imageController { 16 17 @RequestMapping(value="/addImage", method=RequestMethod.POST) 18 public String addImage(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest 19 request, ModelMap model){ 20 System.out.println("开始"); 21 String path = request.getSession().getServletContext().getRealPath("upload"); 22 String fileName = file.getOriginalFilename(); 23 System.out.println(path + "\\" + fileName); 24 File targetFile = new File(path, fileName); 25 if(!targetFile.exists()){ 26 targetFile.mkdirs(); 27 } 28 29 //保存 30 try { 31 file.transferTo(targetFile); 32 } catch (Exception e) { 33 e.printStackTrace(); 34 } 35 model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName); 36 37 return "result"; 38 } 39 40 }
这样就能完成springmvc文件上传功能了。