Spring获取上传文件
<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中实现文件上传
推荐阅读
-
filezilla二进制上传功能介绍,filezilla上传文件操作方法
-
filezilla上传后文件不见了是什么原因? filezilla设置隐藏文件的方法介绍
-
myeclipse创建spring配置文件正确却显示红叉该怎么办?
-
C#调用Rar文件及获取Rar返回值的方法
-
Android上传文件到服务器的方法
-
C#中的文件路径获取函数和文件名字获取函数小结
-
Python批量处理网易云音乐缓存文件获取MP3
-
在ASP.NET 2.0中操作数据之五十二:使用FileUpload上传文件
-
在ASP.NET 2.0中操作数据之五十四:添加新记录时包含一个文件上传选项
-
C#几种获取网页源文件代码的实例