pringMVC实现多文件

pringMVC实现多文件

1.springMVC实现多文件上传需要的包如图SpringMVC实现多文件(批量)上传-LMLPHP2.webroot下的结构如图所示SpringMVC实现多文件(批量)上传-LMLPHP

3.java代码:SpringMVC实现多文件(批量)上传-LMLPHP

 package cn.lxc.controller;

 import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile; @Controller
public class UploadController {
@RequestMapping("/upload.do")
public String upload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest req){
String path= req.getSession().getServletContext().getRealPath("/");
System.out.println(path);
String fileName=file.getOriginalFilename();
try {
InputStream is = file.getInputStream();
OutputStream os = new FileOutputStream(new File(path,fileName));
int len=0;
byte[] buffer = new byte[400];
while((len=is.read(buffer))!=-1){ //len为读取数据的字节长度
os.write(buffer, 0, len); //三个参数分别为输出的字节数组、数据起始偏移量,输出字节长度
}
os.close();
is.close(); } catch (IOException e) {
e.printStackTrace();
}
return "redirect:/index.jsp";
}
}

4.jsp代码 SpringMVC实现多文件(批量)上传-LMLPHP

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<form action="upload.do" method="post" enctype="multipart/form-data">
file1:<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
</body>
</html>

5.springmvc-servlet.xml配置

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 适配器 使用注解 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 渲染器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 文件上传配置 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="104857600"></property>
<property name="maxInMemorySize" value="40960"></property>
</bean>
<!-- 扫描注解包 -->
<context:component-scan base-package="cn.lxc.controller" />
</beans>

6.web.xml配置

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
05-13 00:47