利⽤spring的CommonsMultipartResolver上传⽂件
1、CommonsMultipartResolver是spring⾥⾯提供的⼀个上传⽅式,效率我不知道,但是加⼊spring容器管理还是很不错的。
2、先看依赖包l
<modelVersion>4.0.0</modelVersion>
<groupId&</groupId>
<artifactId>springfile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>apachemons.fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>
说明:因为上传⽂件的这⼀套东西都是apache提供的,在使⽤CommonsMultipartResolver上传⽂件时,需要⽤到相应的依赖包3、l配置
<?xml version="1.0" encoding="UTF-8"?>
<display-name>iframe</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:l</param-value> </init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>filterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filterEncoding</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
</web-app>
l配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:jdbc="/schema/jdbc"
xmlns:jee="/schema/jee"
xmlns:tx="/schema/tx"
xmlns:aop="/schema/aop"
xmlns:mvc="/schema/mvc"
xmlns:util="/schema/util"
xmlns:jpa="/schema/data/jpa"
xsi:schemaLocation="
<mvc:annotation-driven/>
<context:component-scan base-package=""/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
<bean id="commonsMultipartResolver"
class="org.springframework.web.multipartmons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
</bean>
</beans>
5、web端的代码(file.jsp)
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"> <%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/"; %>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
</head>
<body>
<form action="file/upLoad" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
注意:上传的⽂件的input需要写⼊name并且唯⼀
6、java后台
@RequestMapping(value="/file")
public class FileController {
@RequestMapping(value="/init")
public String init(){
return "file";
}
@RequestMapping(value="/upLoad")
@ResponseBody
public void upLoad(HttpServletRequest request) throws Exception{
//创建⼀个多分解的容器
CommonsMultipartResolver cmr = new Session().getServletContext());
//设置编码
cmr.setDefaultEncoding("utf-8");
//判断是否有⽂件上传
param nameif(cmr.isMultipart(request)){
//将request转换成多分解请求
MultipartHttpServletRequest mhs = solveMultipart(request);
//根据input中存在的name来获取是否存在上传⽂件
MultipartFile mf = File("file");#这⾥可以⽤getFiles("file")的⽅式来处理多个⽂件。返回的是⼀个list.然后⼀个⼀个处理就可以了
//创建⽂件保存名
File file = new OriginalFilename());
//上传⽂件
}
}
}
7、效果,因为是相对路径,我直接打开项⽬

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。