Spring MVC在tomcat的主目录中上传文件以进行每次上传,但从未被删除。
有两个问题,
有没有办法将文件上传为从JSP到Spring MVC控制器的byte []
答案 0 :(得分:0)
一种解决方案是扩展AbstractAnnotationConfigDispatcherServletInitializer
覆盖其继承的customizeRegistration()
方法。
可以提供MultipartConfigElement
类的对象以配置与多部分相关的行为:
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
. . .
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
registration.setMultipartConfig(
new MultipartConfigElement(LOCATION, MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD)
);
}
private static final String LOCATION = "C:/temp/"; // Temporary location where files will be stored
private static final long MAX_FILE_SIZE = 1024 * 1024 * 5; // 5MB : Max file size.
// Beyond that size spring will throw exception.
private static final long MAX_REQUEST_SIZE = 1024 * 1024 * 20; // 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
}
请参阅:Spring MVC 4 File Upload Example using Servlet 3 MultiPartConfigElement
答案 1 :(得分:0)
最好有一个单独的外部文件夹用于文件上传,并将该文件夹映射到Tomcat中作为位置。
<Context path="/uploads" docBase="C:/uploads/" />
可从以下网址访问:
localhost:8080/uploads