- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我想检查上传文件的大小并阻止文件完全加载到内存中。我正在使用 CommonsMultipartFile。上传的文件将被处理并保存在数据库中。 AbstractCoupleUploadController 类处理包含文件的传入请求:
public abstract class AbstractCoupleUploadController<T extends Serializable> extends RemoteServiceServlet implements ServletContextAware,
UploadServlet<WorkshopHistoryModel>
{
...
@RequestMapping(method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView handleRequest(@RequestParam("firstFile") CommonsMultipartFile firstFile,
@RequestParam("secondFile") CommonsMultipartFile secondFile, HttpServletRequest request, HttpServletResponse response)
{
synchronized(this)
{
initThreads();
perThreadRequest.set(request);
perThreadResponse.set(response);
}
handleUpload(firstFile,secondFile,request,response);
response.getWriter().flush();
response.flushBuffer();
return null;
}
private void handleUpload(CommonsMultipartFile firstFile, CommonsMultipartFile secondFile, HttpServletRequest request,
HttpServletResponse response) throws IOException
{
response.setContentType("text/html");
if(firstFile.getSize() == 0 || secondFile.getSize() == 0)
{
response.getWriter().print(AppConstants.UPLOAD_ZERO_SIZE_FILE);
return;
}
// other validations
// uploading:
try
{
String content = request.getParameter(CoupleUploadPanel.CONTENT);
T model = deserialize(content);
UploadResultModel resultModel = upload(model,firstFile,secondFile); // it's implemented in UploadFileServletImpl
if(resultModel.hasCriticalError())
{
response.getWriter().print(AppConstants.UPLOAD_FAIL + "," + String.valueOf(resultModel.getWorkshopHistoryId()));
}
else
{
response.getWriter().print(AppConstants.UPLOAD_SUCCESS + "," + String.valueOf(resultModel.getWorkshopHistoryId()));
}
}
catch(ProcessRequestException e)
{
// write upload error description in response.getWriter()
}
catch(Exception e)
{
e.printStackTrace();
response.getWriter().print(AppConstants.UPLOAD_UNKOWN_ERROR);
}
}
...
}
我的 app-servlet.xml 中有一个 multipartResolver bean (file.upload.max_size=9437184),还有一个用于处理 UploadSizeExceededExceptions 的 maxUploadSizeExceededExceptionHandler bean:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="${file.upload.max_size}" />
</bean>
<bean id="maxUploadSizeExceededExceptionHandler" class="com.insurance.ui.server.uploadfile.MaxUploadSizeExceededExceptionHandler">
<property name="order" value="1"/>
</bean>
我的 maxUploadSizeExceededExceptionHandler:
public class MaxUploadSizeExceededExceptionHandler implements HandlerExceptionResolver, Ordered
{
private int order;
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
{
if(ex instanceof MaxUploadSizeExceededException)
{
try
{
response.getWriter().print(ErrorConstants.UPLOAD_SIZE_EXCEED + "," + (((MaxUploadSizeExceededException) ex).getMaxUploadSize()/(1024*1024)));
response.getWriter().flush();
response.flushBuffer();
return new ModelAndView();
}
catch(IOException e)
{
}
}
return null;
}
...
}
当我上传一个非常大的文件(超过 ${file.upload.max_size},大约 700MB)时,CommonsMultipartResolver 立即抛出 MaxUploadSizeExceededException 我正在捕获并处理它(写入 response.getWriter())。但是 < strong>我的问题:我的浏览器上传进度条显示文件还在上传!!为什么?
更新:我正在使用:
也试过了:
还有我的 AS:
更新 2: 在客户端,我使用的是 GWT(我认为没关系):
点击 submitRequestButton 开始上传:
@UiHandler("submitRequestButton")
public void submitRequestButtonClick(ClickEvent event)
{
try
{
// some validation
submitRequestButton.setEnabled(false);
uploadPanel.upload(model.getWorkshopHistoryModel()); // uploadPanel is from the CoupleUploadPanel type
}
catch(ValidationException exception)
{
// handle validation errors
}
catch(SerializationException e)
{
// handle serialization errors
}
}
我有一个 CoupleUploadPanel 小部件用于上传(两个文件):
public class CoupleUploadPanel<T extends Serializable> extends FormPanel
{
public final static String CONTENT = "content";
private static final String FIRST_FILE = "firstFile";
private static final String SECOND_FILE = "secondFile";
private Hidden contentInput;
private FileUpload firstFileUploadInput;
private FileUpload secondFileUploadInput;
private SerializationStreamFactory factory;
public CoupleUploadPanel(UploadServletAsync<T> factory)
{
this(null,factory);
}
public CoupleUploadPanel(String url, UploadServletAsync<T> factory)
{
this.factory = (SerializationStreamFactory) factory;
if(url != null)
{
setAction(url);
}
init();
}
public CoupleUploadPanel(String target, String url, UploadServletAsync<T> factory)
{
super(target);
this.factory = (SerializationStreamFactory) factory;
if(url != null)
{
setAction(url);
}
init();
}
private void init()
{
setMethod("POST");
setEncoding(ENCODING_MULTIPART);
firstFileUploadInput = new FileUpload();
firstFileUploadInput.setName(CoupleUploadPanel.FIRST_FILE);
secondFileUploadInput = new FileUpload();
secondFileUploadInput.setName(CoupleUploadPanel.SECOND_FILE);
contentInput = new Hidden();
contentInput.setName(CONTENT);
VerticalPanel panel = new VerticalPanel();
panel.add(firstFileUploadInput);
panel.add(secondFileUploadInput);
panel.add(contentInput);
add(panel);
}
public void upload(T input) throws SerializationException
{
contentInput.setValue(serialize(input));
submit();
}
private String serialize(T input) throws SerializationException
{
SerializationStreamWriter writer = factory.createStreamWriter();
writer.writeObject(input);
return writer.toString();
}
}
我们应该将 UploadServletAsync 传递给 CoupleUploadPanel 构造函数。 UploadServletAsync 和 UploadServlet 接口(interface):
public interface UploadServletAsync<T extends Serializable>
{
void upload(T model, AsyncCallback<Void> callback);
}
public interface UploadServlet<T extends Serializable> extends RemoteService
{
void upload(T model);
}
所以uploadPanel会以这种方式实例化:
uploadPanel= new CoupleUploadPanel<WorkshopHistoryModel>((UploadFileServletAsync) GWT.create(UploadFileServlet.class));
uploadPanel.setAction(UploadFileServlet.URL);
并且在uploadPanel 中添加了一个SubmitCompeleteHandler(onSumbitComplete() 将在提交完成并将结果传递给客户端时调用):
uploadPanel.addSubmitCompleteHandler(new SubmitCompleteHandler()
{
@Override
public void onSubmitComplete(SubmitCompleteEvent event)
{
String s = event.getResults(); //contains whatever written by response.getWriter()
if(s == null)
{
// navigate to request list page
}
else
{
String[] response = s.split(",");
// based on response:
// show error messages if any error occurred in file upload
// else: navigate to upload result page
}
}
});
UploadFileServlet 和 UploadFileServletAsync 接口(interface):
public interface UploadFileServlet extends UploadServlet<WorkshopHistoryModel>
{
String URL = "**/uploadFileService.mvc";
}
public interface UploadFileServletAsync extends UploadServletAsync<WorkshopHistoryModel>
{
public static final UploadFileServletAsync INSTANCE = GWT.create(UploadFileServlet.class);
}
在服务器端:UploadFileServletImpl扩展了AbstractCoupleUploadController并实现了upload()方法(上传过程):
@RequestMapping(UploadFileServlet.URL)
public class UploadFileServletImpl extends AbstractCoupleUploadController<WorkshopHistoryModel>
{
...
@Override
protected UploadResultModel upload(WorkshopHistoryModel model, MultipartFile firstFile, MultipartFile secondFile)
throws ProcessRequestException
{
return workshopHistoryService.submitList(model.getWorkshop(),firstFile,secondFile);
}
...
}
最佳答案
好吧,afaik Spring(一个 servlet 和一些过滤器)不观察上传过程,而只处理完成过程的结果。这是因为上传是由 Tomcat 自己处理的(提示:web.xml
中有上传大小限制选项)。因此,可能会导致上传失败(Spring 不会注意到)或上传太大的文件。只有当第二次发生时,特定的过滤器/拦截器才能拒绝该过程。
在我上次的设置中,我在 Tomcat 前使用 Nginx 作为代理:
关于java - 处理 MaxUploadSizeExceededException 不能停止上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19717418/
我有一个简单的 spring 应用程序,带有一个需要多部分文件的 Controller 。出于测试目的,我设置了 MaxUploadSize大小仅为 50 字节。当我在 tomcat 中部署 war
MaxUploadSizeExceededException 当我上传的文件大小超过允许的最大值时出现异常。我想在出现此异常时显示错误消息(如验证错误消息)。在 Spring 3 中如何处理此异常以执
我正在开发图像上传系统。我使用 CommonsMultipartResolver 设置 maxUploadSize。当我尝试上传超过最大尺寸的图像文件时,会发生 MaxUploadSizeExcced
所以我使用 Spring Webflow 上传一个文件和带有最大文件大小参数的 CommonsMultipartResolver。 当最大文件大小超过 CommonsMultipartResolver
我想检查上传文件的大小并阻止文件完全加载到内存中。我正在使用 CommonsMultipartFile。上传的文件将被处理并保存在数据库中。 AbstractCoupleUploadControlle
我有一个包含三个文本字段和一个文件上传字段的表单。当我遇到 MaxUploadSizeExceededException 异常时,我可以使用实现 HandlerExceptionResolver 的类
我使用的是 Spring 3.2.0。根据this答案,我在带注释的 Controller 中有相同的方法,它实现了 HandlerExceptionResolver 接口(interface),例如
我正在尝试处理最大上传大小异常,但我发现的所有解决方案都失败了,我尝试实现 HandlerExceptionResolver、@ControllerAdvice 和由 @ExceptionHandle
我有简单的 SpringBoot 应用程序文件上传功能,其中最大文件上传文件大小为 2 MB。 我已经配置了 multipart.max-file-size=2MB 它工作正常。但是当我尝试上传大于
我发现 Apache Tomcat 允许以下配置,采用硬编码或注释方法。我不确定最大文件大小是在上传过程中还是在文件上传到临时位置之后计算的。该文档指示以下内容: The @MultipartConf
我正在使用 Spring Web 4.0.5、Spring Security 3.2.4、Commons FileUpload 1.3.1、Tomcat 7,我得到了一个丑陋的 MaxUploadSi
当超过文件大小时,如何通过文件上传拦截和发送自定义错误消息。我在 Controller 类中有一个带注释的异常处理程序,但请求没有到达 Controller 。我在这个链接上找到的答案 How to
我对此进行了一些研究,结果相互矛盾。要处理此错误,有人说我需要在我的一个 Controller 中实现 HandlerExceptionResolver。 这里有一些链接: How to handle
我有这段代码(就像在 Spring 的引用中): 当用户尝试上传超过 100 kB 的文件时,将显示 HTTP 状态为 500 的服务器错误页面和堆栈跟踪。如何
我无法捕捉和优雅地处理公共(public)文件上传的 FileUploadBase.SizeLimitExceededException或 Spring 的MaxUploadSizeExceededE
我是一名优秀的程序员,十分优秀!