SpringMVC 中,文件的上传,是通过 MultipartResolver 实现的。 所以,如果要实现文件的上传,只要在 spring-mvc.xml 中注册相应的 MultipartResolver 即可。

MultipartResolver 的实现类有两个:

  1. CommonsMultipartResolver
  2. StandardServletMultipartResolver

两个的区别:

  1. 第一个需要使用 Apache 的 commons-fileupload 等 jar 包支持,但它能在比较旧的 servlet 版本中使用。
  2. 第二个不需要第三方 jar 包支持,它使用 servlet 内置的上传功能,但是只能在 Servlet 3 以上的版本使用。

第一个使用步骤:

/*CommonsMultipartResolver  上传用到的两个包*/
"commons-fileupload:commons-fileupload:1.3.1",
"commons-io:commons-io:2.4"


Spring_mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
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">

<!-- 开启mvc-->
<mvc:annotation-driven/>

<!-- 配置扫描发现所有具有 @Controller 注解的类,加载到容器 -->
<context:component-scan base-package="text1"/>


<bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp"/>
</bean>


<!--1 CommonsMultipartResolver
第一个需要使用 Apache 的 commons-fileupload 等 jar 包支持,
但它能在比较旧的 servlet 版本中使用-->

<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
</bean>



</beans>




 

 

 

web.xml




<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring_mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>




</servlet>

<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>



 

imgTest .java


java




package text1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/create")
public class imgTest {

@Autowired
private HttpServletRequest request;


@RequestMapping("/jq")
public String jq() {
System.out.println("oooo");
return "index";
}

@RequestMapping("/upload")
public String upload(MultipartFile[] mfile) throws IOException {

if (mfile !=null && mfile.length>0) {
for (int i = 0;i<mfile.length;i++){
long start = System.currentTimeMillis();
System.out.println("-------------------------------------------------");
System.out.println("获取文件流"+mfile[i].getInputStream());
System.out.println("获取文件的字节大小【byte】"+mfile[i].getSize());
System.out.println("文件类型"+mfile[i].getContentType());
System.out.println("获取文件数据"+mfile[i].getBytes());
System.out.println("文件名字"+mfile[i].getName());
System.out.println("获取上传文件的原名"+mfile[i].getOriginalFilename());

System.out.println("-------------------------------------------------");

try {
String filePath = request.getSession().getServletContext()
.getRealPath("/") + "assets/" +start+ mfile[i].getOriginalFilename();

//转存文件

mfile[i].transferTo(new File(filePath));
}catch (Exception e){
e.printStackTrace();
}

//mfile[i].transferTo(new File("D:/ideas/"+mfile[i].getOriginalFilename()+ mfile[i].getOriginalFilename().substring(
// mfile[i].getOriginalFilename().lastIndexOf("."))));
// System.out.println(mfile.getOriginalFilename());
// mfile[i].transferTo(new File("/assets" + mfile[i].getOriginalFilename()));


} return "cg";

} else {
System.out.println("失败");
return "sb";
}
}
}




 

html:




<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>

<title>Title</title>
</head>
<body>
<form:form action="/create/upload" enctype="multipart/form-data">
<input type="file" name="mfile" id="img" /><br>
<input type="file" name="mfile" id="img2"/>
<%--<img src="#" id="ser" >--%>
<input type="submit" value="上传图片" />
</form:form>

</body>
</html>




 

MultipartFile(文件的上传)--CommonsMultipartResolver_上传

 

 

 

 

第二个使用步骤: 这个就不要导包---建议用这个 

 

Spring_mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
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">

<!-- 开启mvc-->
<mvc:annotation-driven/>

<!-- 配置扫描发现所有具有 @Controller 注解的类,加载到容器 -->
<context:component-scan base-package="text1"/>


<bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp"/>
</bean>





<!--2 注册上传 StandardServletMultipartResolver
第二个不需要第三方 jar 包支持,它使用 servlet 内置的上传功能,
但是只能在 Servlet 3 以上的版本使用。
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">

</bean>


</beans>




 

web.xml




<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring_mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>


<multipart-config>
<!-- 上传文件的大小限制,比如下面表示 5 M -->
<max-file-size>5242880</max-file-size>
<!-- 一次表单提交中文件的大小限制,必须下面代表 10 M -->
<max-request-size>10485760</max-request-size>
<!-- 多大的文件会被自动保存到硬盘上。0 代表所有 -->
<file-size-threshold>0</file-size-threshold>
</multipart-config>

</servlet>

<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>



其他的都是一样的。

imgTest .java  和html 这个代码都是和上面的不变!