1.springMVC实现多文件上传需要的包如图2.webroot下的结构如图所示
3.java代码:
1 package cn.lxc.controller; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 9 import javax.servlet.http.HttpServletRequest; 10 11 import org.springframework.stereotype.Controller; 12 import org.springframework.web.bind.annotation.RequestMapping; 13 import org.springframework.web.bind.annotation.RequestParam; 14 import org.springframework.web.multipart.commons.CommonsMultipartFile; 15 16 @Controller 17 public class UploadController { 18 @RequestMapping("/upload.do") 19 public String upload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest req){ 20 String path= req.getSession().getServletContext().getRealPath("/"); 21 System.out.println(path); 22 String fileName=file.getOriginalFilename(); 23 try { 24 InputStream is = file.getInputStream(); 25 OutputStream os = new FileOutputStream(new File(path,fileName)); 26 int len=0; 27 byte[] buffer = new byte[400]; 28 while((len=is.read(buffer))!=-1){ //len为读取数据的字节长度 29 os.write(buffer, 0, len); //三个参数分别为输出的字节数组、数据起始偏移量,输出字节长度 30 } 31 os.close(); 32 is.close(); 33 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 return "redirect:/index.jsp"; 38 } 39 }
4.jsp代码
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 <title>My JSP 'index.jsp' starting page</title> 12 </head> 13 <body> 14 <form action="upload.do" method="post" enctype="multipart/form-data"> 15 file1:<input type="file" name="file"/> 16 <input type="submit" value="上传"/> 17 </form> 18 </body> 19 </html>
5.springmvc-servlet.xml配置
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 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 13 <!-- 渲染器 --> 14 <bean id="viewResolver" 15 class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 16 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 17 <property name="prefix" value="/WEB-INF/jsp/"/> 18 <property name="suffix" value=".jsp"/> 19 </bean> 20 <!-- 文件上传配置 --> 21 <bean id="multipartResolver" 22 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 23 <property name="defaultEncoding" value="utf-8"></property> 24 <property name="maxUploadSize" value="104857600"></property> 25 <property name="maxInMemorySize" value="40960"></property> 26 </bean> 27 <!-- 扫描注解包 --> 28 <context:component-scan base-package="cn.lxc.controller" /> 29 </beans>
6.web.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <servlet> 4 <servlet-name>springmvc</servlet-name> 5 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 6 <load-on-startup>1</load-on-startup> 7 </servlet> 8 <servlet-mapping> 9 <servlet-name>springmvc</servlet-name> 10 <url-pattern>*.do</url-pattern> 11 </servlet-mapping> 12 </web-app>