我的控制器中有一个非常基本的上传操作。它看起来像下面的动作。它很棒。我看到的唯一问题是用户取消上传(或在浏览器上点击停止)。我能够正确恢复,但在我的日志中看到未捕获的异常之前没有。下面列出了例外情况。任何有关如何正确捕获未捕获的异常的帮助或反馈将不胜感激。似乎它正在客户端和控制器操作之间发生,因为正在显示异常,但操作中没有任何日志消息显示出来。
def upload = {
def f = null
try {
f = request.getFile('assetFile')
if(!f || f.empty) {
log.warn "File is empty"
render(view:'upload')
return
}
} catch (Exception e) {
log.warn "Caught exception:", e
render(view:'upload')
return
}
}
例外是:
2010-08-06 15:33:22,826 ERROR [TP-Processor8] filter.UrlMappingsFilter - Error when matching URL mapping [/(*)/(*)?/(*)?]:Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.jsecurity.web.servlet.JSecurityFilter.doFilterInternal(JSecurityFilter.java:384)
at org.jsecurity.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:183)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:200)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:775)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:704)
at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:897)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:367)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
... 25 more
Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:983)
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:887)
at java.io.InputStream.read(InputStream.java:85)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:94)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:64)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:362)
... 26 more
答案 0 :(得分:2)
我在这方面非常晚了,但我今天遇到了同样的问题并通过添加servlet过滤器来解决它(我也尝试过Grails过滤器,但在点击之前抛出异常)。
首先,您需要在项目中创建一个web.xml ...
grails install-templates
向web.xml添加过滤器(确保不要在任何其他过滤器之前放置过滤器映射)...
<filter>
<filter-name>upload</filter-name>
<filter-class>com.myProject.UploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>upload</filter-name>
<url-pattern>/media/uploadMediaAsset/*</url-pattern>
</filter-mapping>
创建过滤器类...
package com.myProject
import javax.servlet.*
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.web.multipart.MultipartException
public class UploadFilter implements Filter {
private Log log = LogFactory.getLog(getClass())
public void init(FilterConfig filterConfig) throws ServletException { /* Do nothing */ }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException {
log.debug 'Making sure file upload gets here.'
try {
chain.doFilter(request, response)
} catch (MultipartException mpE) {
log.error mpE
}
}
public void destroy() { /* Do nothing */ }
}
答案 1 :(得分:1)
迟到的回应。我实际上能够找到仍在解决这个问题的人的解决方案。请参阅http://brainflush.wordpress.com/2008/12/04/how-to-gracefully-recover-from-file-upload-errors-in-grails/。