【本系列其他教程正在陆续翻译中,点击分类:spring 4 mvc 进行查看。源码下载地址在文章末尾。】
【翻译 by 明明如月 QQ 605283073】
原文地址:http://websystique.com/springmvc/spring-mvc-4-file-upload-example-using-multipartconfigelement/
上一篇:Spring MVC 4 使用常规的fileupload上传文件(带源码)
本文在Servlet 3 环境下使用实现了StandardServletMultipartResolver 接口的SpringMultipartResolver类,来实现单个或者多文件上传。
Spring 提供内置的multipart 来支持web应用的文件上传。
前面的文章讲述了用 CommonsMultipartResolver实现文件上传. 在Servlet 3.0 环境下甚至都很好用.本文也将实现同样的上传效果, 但在Servlet 3.0 指定用javax.servlet.MultipartConfigElement
在Servlet 3.0作用要在 Spring 里激活Multipart。需要下面的步骤:
1. 在 Spring配置中添加StandardServletMultipartResolver
Bean.它是基于ased on the Servlet 3.0 javax.servlet.http.Part API的MultipartResolver接口的标准实现类,
2. Servlet 3.0环境下启用MultiParsing . 要做到这样,你有很多选择.
javax.servlet.MultipartConfigElement
. MultipartConfigElement是javax.servlet.annotation.MultipartConfig注解的表示
(和选项 c中类似). 本文中将用此种方法。
SpringDispatcher
org.springframework.web.servlet.DispatcherServlet
/tmp
5242880
20971520
0
选项C. 你可以通过javax.servlet.annotation.MultipartConfig
注解来自定义Servlet,如下:
@WebServlet(name = "fileUploadServlet", urlPatterns = {"/upload"})
@MultipartConfig(location=/tmp,
fileSizeThreshold=0,
maxFileSize=5242880, // 5 MB
maxRequestSize=20971520) // 20 MB
public class FileUploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//handle file upload
}
使用的技术和软件
4.0.0
com.websystique.springmvc
Spring4MVCFileUploadMultipartResolverExample
war
1.0.0
Spring4MVCFileUploadMultipartConfigElementExample Maven Webapp
http://maven.apache.org
4.2.0.RELEASE
org.springframework
spring-webmvc
${springframework.version}
javax.validation
validation-api
1.1.0.Final
javax.servlet
javax.servlet-api
3.1.0
javax.servlet
jstl
1.2
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.7
1.7
org.apache.maven.plugins
maven-war-plugin
2.4
src/main/webapp
Spring4MVCFileUploadMultipartConfigElementExample
false
Spring4MVCFileUploadMultipartConfigElementExample
注册MultiPartConfigElement提供定制最大文件大小、请求大小等。在上传文件才做时以及存在本地临时文件的位置和入口。
package com.websystique.springmvc.configuration;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class HelloWorldInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class[] getRootConfigClasses() {
return new Class[] { HelloWorldConfiguration.class };
}
@Override
protected Class[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
registration.setMultipartConfig(getMultipartConfigElement());
}
private MultipartConfigElement getMultipartConfigElement() {
MultipartConfigElement multipartConfigElement = new MultipartConfigElement( LOCATION, MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD);
return multipartConfigElement;
}
private static final String LOCATION = "C:/temp/"; // Temporary location where files will be stored
private static final long MAX_FILE_SIZE = 5242880; // 5MB : Max file size.
// Beyond that size spring will throw exception.
private static final long MAX_REQUEST_SIZE = 20971520; // 20MB : Total request size containing Multi part.
private static final int FILE_SIZE_THRESHOLD = 0; // Size threshold after which files will be written to disk
}
注意,我们重写了customizeRegistration 方法将MultiPartConfigElement注册到DispatcherServlet
in。
创建 StandardServletMultipartResolver
Bean。
它是基于 Servlet 3.0 javax.servlet.http.Part API的MultipartResolver 接口的实现类。
package com.websystique.springmvc.configuration;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.websystique.springmvc")
public class HelloWorldConfiguration extends WebMvcConfigurerAdapter {
@Bean(name = "multipartResolver")
public StandardServletMultipartResolver resolver() {
return new StandardServletMultipartResolver();
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
registry.viewResolver(viewResolver);
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations( "/static/");
}
}
对应的XML配置:
messages
/WEB-INF/views/
.jsp
package com.websystique.springmvc.model;
import org.springframework.web.multipart.MultipartFile;
public class FileBucket {
MultipartFile file;
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}
包装类:
package com.websystique.springmvc.model;
import java.util.ArrayList;
import java.util.List;
public class MultiFileBucket {
List files = new ArrayList();
public MultiFileBucket(){
files.add(new FileBucket());
files.add(new FileBucket());
files.add(new FileBucket());
}
public List getFiles() {
return files;
}
public void setFiles(List files) {
this.files = files;
}
}
此类最多可以处理3个文件上传
package com.websystique.springmvc.controller;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import com.websystique.springmvc.model.FileBucket;
import com.websystique.springmvc.model.MultiFileBucket;
import com.websystique.springmvc.util.FileValidator;
import com.websystique.springmvc.util.MultiFileValidator;
@Controller
public class FileUploadController {
private static String UPLOAD_LOCATION="C:/mytemp/";
@Autowired
FileValidator fileValidator;
@Autowired
MultiFileValidator multiFileValidator;
@InitBinder("fileBucket")
protected void initBinderFileBucket(WebDataBinder binder) {
binder.setValidator(fileValidator);
}
@InitBinder("multiFileBucket")
protected void initBinderMultiFileBucket(WebDataBinder binder) {
binder.setValidator(multiFileValidator);
}
@RequestMapping(value = { "/", "/welcome" }, method = RequestMethod.GET)
public String getHomePage(ModelMap model) {
return "welcome";
}
@RequestMapping(value = "/singleUpload", method = RequestMethod.GET)
public String getSingleUploadPage(ModelMap model) {
FileBucket fileModel = new FileBucket();
model.addAttribute("fileBucket", fileModel);
return "singleFileUploader";
}
@RequestMapping(value = "/singleUpload", method = RequestMethod.POST)
public String singleFileUpload(@Valid FileBucket fileBucket,
BindingResult result, ModelMap model) throws IOException {
if (result.hasErrors()) {
System.out.println("validation errors");
return "singleFileUploader";
} else {
System.out.println("Fetching file");
MultipartFile multipartFile = fileBucket.getFile();
// Now do something with file...
FileCopyUtils.copy(fileBucket.getFile().getBytes(), new File( UPLOAD_LOCATION + fileBucket.getFile().getOriginalFilename()));
String fileName = multipartFile.getOriginalFilename();
model.addAttribute("fileName", fileName);
return "success";
}
}
@RequestMapping(value = "/multiUpload", method = RequestMethod.GET)
public String getMultiUploadPage(ModelMap model) {
MultiFileBucket filesModel = new MultiFileBucket();
model.addAttribute("multiFileBucket", filesModel);
return "multiFileUploader";
}
@RequestMapping(value = "/multiUpload", method = RequestMethod.POST)
public String multiFileUpload(@Valid MultiFileBucket multiFileBucket,
BindingResult result, ModelMap model) throws IOException {
if (result.hasErrors()) {
System.out.println("validation errors in multi upload");
return "multiFileUploader";
} else {
System.out.println("Fetching files");
List fileNames = new ArrayList();
// Now do something with file...
for (FileBucket bucket : multiFileBucket.getFiles()) {
FileCopyUtils.copy(bucket.getFile().getBytes(), new File(UPLOAD_LOCATION + bucket.getFile().getOriginalFilename()));
fileNames.add(bucket.getFile().getOriginalFilename());
}
model.addAttribute("fileNames", fileNames);
return "multiSuccess";
}
}
}
将宝存在 C:/mytemp文件夹。
package com.websystique.springmvc.util;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import com.websystique.springmvc.model.FileBucket;
@Component
public class FileValidator implements Validator {
public boolean supports(Class clazz) {
return FileBucket.class.isAssignableFrom(clazz);
}
public void validate(Object obj, Errors errors) {
FileBucket file = (FileBucket) obj;
if(file.getFile()!=null){
if (file.getFile().getSize() == 0) {
errors.rejectValue("file", "missing.file");
}
}
}
}
package com.websystique.springmvc.util;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import com.websystique.springmvc.model.FileBucket;
import com.websystique.springmvc.model.MultiFileBucket;
@Component
public class MultiFileValidator implements Validator {
public boolean supports(Class clazz) {
return MultiFileBucket.class.isAssignableFrom(clazz);
}
public void validate(Object obj, Errors errors) {
MultiFileBucket multiBucket = (MultiFileBucket) obj;
int index=0;
for(FileBucket file : multiBucket.getFiles()){
if(file.getFile()!=null){
if (file.getFile().getSize() == 0) {
errors.rejectValue("files["+index+"].file", "missing.file");
}
}
index++;
}
}
}
messages.properties
missing.file= Please select a file.
singleFileUploader.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
Spring 4 MVC File Upload Example
Spring 4 MVC File Upload Example
Upload a file
Home
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
File Upload Success
File ${fileName} uploaded successfully.
Home
multiFileUploader.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
Spring 4 MVC File Multi Upload Example
Spring 4 MVC Multi File Upload Example
Home
multiSuccess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
File Upload Success
File ${fileName} uploaded successfully
Home
welcome.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Spring 4 MVC File Upload Example
Welcome to FileUploader Example
Click on below links to see FileUpload in action.
Single File Upload OR Multi File Upload
在tomcat里发布运行
浏览器输入: http://localhost:8080/Spring4MVCFileUploadMultipartConfigElementExample/ 回车
选择单文件上传链接
如果没有选择文件直接点上传会有验证提示
选择一个文件
点击上传
可以去C:/mytemp 文件夹查看上传的文件
选择多文件上传
如果不选择 直接点击上传
选择文件
点击:上传
检查保存的文件夹C:/mytemp
项目文件下载:http://websystique.com/?smd_process_download=1&download_id=1759
特别说明:此系列教程有的童鞋下载下来运行 经常404 或者改成xml方式以后