springMvc文件上传的细节
创建maven Web工程,导入相关springMvc 的相关坐标
<packaging>war</packaging>
<name>Fspring_Mvc_test Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>Fspring_Mvc_test</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
复制即可
完善工程主干创建 java, resources 文件夹
创建配置文件 : springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="cn.itcast"/>
<!-- 视图解析器对象 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--<!–前端控制器,哪些静态资源不拦截–>-->
<!--<mvc:resources location="/css/" mapping="/css/**"/>-->
<!--<mvc:resources location="/images/" mapping="/images/**"/>-->
<!--<mvc:resources location="/js/" mapping="/js/**"/>-->
<!--配置文件解析器对象 -->
<!--<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760" />
</bean>-->
<!--配置文件解析器 注意id名必须为multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/>
</bean>
<!-- 开启SpringMVC框架注解的支持 -->
<mvc:annotation-driven />
</beans>
配置前端控制器 和 过滤器
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置解决中文乱码的过滤器-->
<filter>
<filter-name>characterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
jsp前端 for表单 属性 详解
method=“post” 必须为post 请求
enctype=“multipart/form-data” 必须指定此属性
input type属性为 file
name属性非常关键 在进行 使用springMvc 方式 和 跨服务上传时 MultipartFile 形参名 必须和 name属性名相对应!!!!
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="str/upload" method="post" enctype="multipart/form-data">
文件<input type="file" name="upload"><br>
<input type="submit" value="上传">
</form>
<h3>springmvc方式上传</h3>
<form action="str/upload2" method="post" enctype="multipart/form-data">
文件<input type="file" name="upload2"><br>
<input type="submit" value="上传">
</form>
<h3>跨服务器的方式上传</h3>
<form action="str/upload3" method="post" enctype="multipart/form-data">
文件<input type="file" name="upload3"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
创建 controller 类
success是我要跳转的页面 在 /WEB-INF/pages/success.jsp 成功页面
@Controller
@RequestMapping("/str")
public class scController {
@RequestMapping("/upload3")
public String testysupload3( MultipartFile upload3) throws Exception {
System.out.println ("spring跨服务文件开始上传....");
//使用原始的文件上传
String path = "http://localhost:9090/upload/";
System.out.println (path);
String filename = upload3.getOriginalFilename ();
String uuid = UUID.randomUUID ().toString ().replace ("-", "");
filename=uuid+"_"+filename;
//跨服务器上传
//创建客户端对象
Client client = Client.create ();
//连接服务器端
WebResource resource = client.resource (path+filename);
//上传文件
resource.put (upload3.getBytes ());
return "success";
}
@RequestMapping("/upload")
public String testysupload(HttpServletRequest request) throws Exception {
System.out.println ("文件开始上传....");
//使用原始的文件上传
String path = request.getSession ().getServletContext ().getRealPath ("/upload/");
System.out.println (path);
//创建file对象
File file =new File (path);
if (!file.exists ()){
file.mkdirs ();
}
//使用工具类
DiskFileItemFactory factory=new DiskFileItemFactory ();
ServletFileUpload upload=new ServletFileUpload (factory);
//使用request 获取
List<FileItem> items = upload.parseRequest (request);
//遍历
for (FileItem item : items) {
if (item.isFormField ()){
//普通表单项
}else {
//
String filename = item.getName ();
//
String uuid = UUID.randomUUID ().toString ().replace ("-", "");
filename=uuid+"_"+filename;
item.write (new File (path,filename));
item.delete ();
}
}
return "success";
}
@RequestMapping("/upload2")
public String testysupload2(HttpServletRequest request, MultipartFile upload2) throws Exception {
System.out.println ("springmvc文件开始上传....");
//使用原始的文件上传
String path = request.getSession ().getServletContext ().getRealPath ("/upload/");
System.out.println (path);
//创建file对象
File file =new File (path);
if (!file.exists ()){
file.mkdirs ();
}
//使用工具类
String filename = upload2.getOriginalFilename ();
String uuid = UUID.randomUUID ().toString ().replace ("-", "");
filename=uuid+"_"+filename;
upload2.transferTo (new File (path,filename));
return "success";
}
}
同志们 一键三连 !!!