<form id="uploadFile" enctype="multipart/form-data"> <input type="file" multiple="multiple" data-default-file="" data-max-file-size="3M"/> </form>
Spring如何获取form中上传的文件呢,Servlet3.0之前的版本,和Servlet3.0是有差别的
请看这段注释
/** * Standard implementation of the {@link MultipartResolver} interface, * based on the Servlet 3.0 {@link javax.servlet.http.Part} API. * To be added as "multipartResolver" bean to a Spring DispatcherServlet context, * without any extra configuration at the bean level (see below). * * <p><b>Note:</b> In order to use Servlet 3.0 based multipart parsing, * you need to mark the affected servlet with a "multipart-config" section in * {@code web.xml}, or with a {@link javax.servlet.MultipartConfigElement} * in programmatic servlet registration, or (in case of a custom servlet class) * possibly with a {@link javax.servlet.annotation.MultipartConfig} annotation * on your servlet class. Configuration settings such as maximum sizes or * storage locations need to be applied at that servlet registration level; * Servlet 3.0 does not allow for them to be set at the MultipartResolver level. * * @author Juergen Hoeller * @since 3.1 */
org.springframework.web.multipart.support.StandardServletMultipartResolver
Servlet3.0及以后,可以用这个StandardServletMultipartResolver来处理上传文件
org.springframework.web.multipart.commons.CommonsMultipartResolver
而3.0之前的版本通常用的是CommonsMultipartResolver来处理
这两者有什么区别呢,新版是基于Servlet3.0的API实现的,旧版是基于Apache Commons FileUpload实现的
两者使用的时候配置也是不同的,老版的CommonsMultipartResolver可以在用的时候配置,而新版的StandardServletMultipartResolver必须在注册Servlet的时候就进行配置。
CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setMaxInMemorySize(maxInMemorySize); resolver.setMaxUploadSize(maxUploadSize); resolver.setDefaultEncoding(defaultEncoding); resolver.setUploadTempDir(uploadTempDir);
基于Apache Commons FileUpload的CommonsMultipartResolver的相关配置网上有很多
通常可以在MultipartResolver的级别进行配置
* In order to use Servlet 3.0 based multipart parsing, * you need to mark the affected servlet with a "multipart-config" section in * {@code web.xml}, or with a {@link javax.servlet.MultipartConfigElement} * in programmatic servlet registration, or (in case of a custom servlet class) * possibly with a {@link javax.servlet.annotation.MultipartConfig} annotation * on your servlet class. Configuration settings such as maximum sizes or * storage locations need to be applied at that servlet registration level; * Servlet 3.0 does not allow for them to be set at the MultipartResolver level.
但是基于Servlet3.0的API实现的StandardServletMultipartResolver需要在web.xml或者在配置Servlet的时候进行配置,不允许在MultipartResolver的级别进行配置。
你可以在注册Servlet的时候加入一个javax.servlet.MultipartConfigElement来进行配置,或者使用javax.servlet.annotation.MultipartConfig注解在servlet类中进行配置。
比如这样:
ServletRegistration.Dynamic registration = container.addServlet("dispatcher", dispatcherServlet); MultipartConfigElement config = new MultipartConfigElement(BaseConst.UPLOAD_FOLDER, 8000L, 8000L, 0); registration.setMultipartConfig(config); registration.setLoadOnStartup(1); registration.addMapping("/");
配置了上传文件最大不超过8Kb
import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver;
@Bean public MultipartResolver multipartResolver(){ StandardServletMultipartResolver resolver = new StandardServletMultipartResolver(); return resolver; }
配置完后在ApplicationContext中实例化一个StandardServletMultipartResolver就可以了
@Service("getUploadFile") public class GetUploadFile { @Autowired private MultipartResolver multipartResolver; public void handleUploadFile(HttpServletRequest request) { MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request); multipartRequest.getFileNames().forEachRemaining(name -> { MultipartFile file = multipartRequest.getFile(name); file.getInputStream();//do something file.getBytes();//do something }); } }
这是使用的例子,非常方便,新老版本没什么区别。
import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartResolver; @Service("getUploadFile") public class GetUploadFile<T> implements IGetUploadFile<T> { @Autowired private MultipartResolver multipartResolver; @Override public List<T> getUploadFile(HttpServletRequest request, IMultipartFileHandler<T> handler) { MultipartHttpServletRequest multipartRequest = multipartResolver .resolveMultipart(request); List<T> uploadFiles = new ArrayList<>(); multipartRequest.getFileNames().forEachRemaining(name -> { MultipartFile file = multipartRequest.getFile(name); T entity = handler.handle(file); uploadFiles.add(entity); }); return uploadFiles; } }
import java.util.List; import javax.servlet.http.HttpServletRequest; public interface IGetUploadFile<T> { /** * get upload files and process * return the result list * as the process method being offered: IMultipartFileHandler<T> * @param request * @param handler * @return */ public List<T> getUploadFile(HttpServletRequest request, IMultipartFileHandler<T> handler); }
import org.springframework.web.multipart.MultipartFile; @FunctionalInterface public interface IMultipartFileHandler<T> { public T handle(MultipartFile file); }
封装成接口来使用,代码更简洁,更易读。
@Autowired private IGetUploadFile<T> iGetUploadFile;
只需注入一下,就可以复用代码,获取上传的文件了。
以上
相关推荐
Spring MVC文件上传下载,是http://blog.csdn.net/geloin/article/details/7537425#comments的配套资源。
前几天在网上找springmvc 文件上传显示进度大部分找的都有问题,然后自己结合网上资料写了一个可以实现的功能,现在已经用在向项目中
非常详细的描述了表单上传文件以及携带表单数据的案例,含有注释,前后端的代码,不懂可以加我问,随时回答
上传文件 下载文件 读取桶列表 读取桶中的文件列表 删除桶 删除文件 Spring boot整合MinIO客户端实现文件管理 Spring boot整合MinIO客户端实现文件管理 Spring boot整合MinIO客户端实现文件管理 Spring boot整合...
JAVA获取FTP文件列表,功能齐全。包括:链接到服务器,上传文件到FTP服务器,从FTP服务器上下载文件并返回下载文件长度,返回FTP目录下的文件列表,删除FTP上的文件及目录,关闭链接。
java web前端上传文件到后台常用三种方式,包括from表单提交、ajax提交、FormData对象提交,框架采用Spring Boot。
使用Spring Boot上传文件的简单Demo
SpringMVC文件上传与下载的实现,实际开发过程中由于使用的Spring版本比较低的,所以一些最新的注解以及方法不能使用,然后使用了一些替代的方法,另外在项目中使用到的路径是从内存中读取的并不是项目路径,具体的...
完整实现ftp上传与下载并解析csv文件,并实现对文件内容进行分析及错误信息的提示及保存日志,并可以实现多个文件同时下载,并且同时解析
本人自己学习springmvc学习文件上传下载,和spring资源国际化
前端Excel大文件file slice分片,md5校验文件完整性并作文件标识记录写入数据库,支持断点...文件上传完毕后,使用EasyExcel读取文件流,处理Excel数据写入数据库中,可处理百万级数据。项目完整,连接数据库即可运行。
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...
此外lazy="true"说明地返回整个Tfile对象时,并不返回fileContent这个字段的数据,只有在显式调用tfile.getFileContent()方法时才真正从数据库中获取fileContent的数据。这是Hibernate3引入的新特性,对于包含重量...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
1、本项目为读取E盘根目录下名为new_datad.txt的json数组文件存入数据库 2、大环境 windows操作系统 jdk:1.7.0_21 tomcat7.0 eclipse Mars.2 Release (4.5.2) 数据库:mysql 5.7.12 数据库名 wordpress 编码...
Excel 到对象绑定Spring Excel 上传文件到对象集合绑定这是所有想要实现解决方案的程序员的事情上传文件(此处为 Excel) 使用 Apache POI 解析文件将 excel 行绑定到 POJO 的集合(重新)在动态绑定中使用 POJO ...
利用Hibernate+SpringMVC+spring写的一个上传一个Excel表并解析里面的数据保存到数据库,再显示到页面。
Springboot通过MinIO进行文件上传、文件下载、删除文件的功能代码,以及图片视频文件的预览功能代码
最近写了关于图片上传至本地文件夹或服务器,上传路径到数据库,并在上传时预览图片。本文通过实例代码给大家分享spring boot2.0图片上传至本地或服务器并配置虚拟路径的方法,需要的朋友参考下
主要介绍了SpringBoot文件上传控制的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下