转自:https://www.cnblogs.com/896240130Master/p/6430908.html

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>

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

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 这个代码都是和上面的不变!

MultipartFile(文件的上传)--CommonsMultipartResolver的更多相关文章

  1. MultipartFile(文件的上传)

    摘自:https://www.cnblogs.com/896240130Master/p/6430908.html https://blog.csdn.net/kouwoo/article/detai ...

  2. 在SpringMVC框架下实现文件的 上传和 下载

    在eclipse中的javaEE环境下:导入必要的架包 web.xml的配置文件: <?xml version="1.0" encoding="UTF-8" ...

  3. SpringMVC+SwfUpload进行多文件同时上传

    由于最近项目需要做一个多文件同时上传的功能,所以好好的看了一下各种上传工具,感觉uploadify和SwfUpload的功能都比较强大,并且使用起来也很方便.SWFUpload是一个flash和js相 ...

  4. SpringMVC进行文件的上传以及多文件的上传(转)

    基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下如何使用SpringMVC进行表单上的文件上传以及多个文件同时上传的步骤 SpringMVC 基础教程 框架分析:htt ...

  5. SpringMVC框架(四)文件的上传下载,上下文路径

    文件目录: SpringMVC配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...

  6. 使用springmvc进行文件的上传和下载

    文件的上传 SpringMVC支持文件上传组件,commons-fileupload,commons-fileupload依赖commons-io组件 配置步骤说明 第一步:导入包 commons-f ...

  7. Spring MVC 实现文件的上传和下载 (八)

    完整的项目案例: springmvc.zip 目录 SpringMVC 中,文件的上传,是通过 MultipartResolver 实现的. 所以,如果要实现文件的上传,只要在 spring-mvc. ...

  8. Spring实现文件的上传下载

    背景:之前一直做的是数据库的增删改查工作,对于文件的上传下载比较排斥,今天研究了下具体的实现,发现其实是很简单.此处不仅要实现单文件的上传,还要实现多文件的上传. 单文件的下载知道了,多文件的下载呢? ...

  9. java web 实现文件夹上传(保留目录结构)

    今天我弄了一下文件夹上传(很简单的 首先,我们的html需要这样写 <form action="/file/upload" enctype="multipart/f ...

随机推荐

  1. [NOIP2003] 提高组 洛谷P1038 神经网络

    题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神经网络的研究一直是当今 ...

  2. python(5)- 基础数据类型

    一 int 数字类型 #abs(x) 返回数字的绝对值,如abs(-10) 返回 10 # ceil(x) 返回数字的上入整数,如math.ceil(4.1) 返回 5 # cmp(x, y) 如果 ...

  3. Mysql数据库的事物

    一 .事物的特性:ACID 数据库的事务必须具备ACID特性,ACID是指 Atomicity(原子性).Consistensy(一致性).Isolation(隔离型)和Durability(持久性) ...

  4. HDU1087 Super Jumping! Jumping! Jumping!(LIS)

    题目意思: http://acm.hdu.edu.cn/showproblem.php? pid=1087 此题的意思求最长上升子序列的和. 题目分析: 在求最长上升子序列的时候,不在保存最长的个数, ...

  5. 去掉小程序textarea上的完成按钮栏

    小程序textarea上会自动多一个完成按钮,如下图所示,如果是mpVue,在textarea添加     :show-confirm-bar="false"     即可.  

  6. Unable to connect to database server to retrieve database list; Arcgis 连接不上postsql库;

    在C:\Program Files (x86)\ArcGIS\Desktop10.2\bin 目录下添加 pg依赖的插件 插件下载地址:

  7. LoadRunner 中调用c函数生成随机字符串

    Action() { int itera_num,rand_num,i; ]=""; char StrTable[]="abcdefghijklmnopqrstuvwxy ...

  8. 优化 html 标签 为何能用HTML/CSS解决的问题就不要使用JS?

    优化 html 标签 2018年05月11日 08:56:24 阅读数:19 有些人写页面会走向一个极端,几乎页面所有的标签都用div,究其原因,用div有很多好处,一个是div没有默认样式,不会有m ...

  9. 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用

    责任链模式的具体应用   1.业务场景 生产车间中使用的条码扫描,往往一把扫描枪需要扫描不同的条码来处理不同的业务逻辑,比如,扫描投入料工位条码.扫描投入料条码.扫描产出工装条码等,每种类型的条码位数 ...

  10. JavaSE----API之集合(Collection、List及其子类、Set及其子类、JDK1.5新特性)

    5.集合类 集合类的由来: 对象用于封装特有数据,对象多了须要存储:假设对象的个数不确定.就使用集合容器进行存储. 集合容器由于内部的数据结构不同,有多种详细容器.不断的向上抽取,就形成了集合框架. ...