文件下载对话框中的IllegalStateException

时间:2013-04-27 23:00:39

标签: java struts2 illegalstateexception

我正在尝试编写一个简单的Struts2类来下载视频文件。它工作正常,除非我选择在下载对话框中取消下载,因为抛出java.lang.IllegalStateException(页面正常工作,会记录异常)。

这是班级:

public class FileDownloaderAction extends ActionSupport {

/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;

/** The relative route to the file to download. */
private String relRoute;

/** The name of the file to download. */
private String fileName;

/** The InputStream to be downloaded. */
private InputStream fileStreamToDownload;

/** The size of the file to download. */
private long fileSize;

@Override
public final String execute() throws Exception {
    MyUtils mu = new MyUtils();

    try {
        File file = mu.getFile(relRoute + fileName);

        if (file != null) {
            fileStreamToDownload = new FileInputStream(file);
            fileSize = file.length();

            return Action.SUCCESS;
        } else {
            return Action.ERROR;
        }
    } catch (Exception ex) {
        return Action.ERROR;
    }
}

/** Sets the content disposition.
 * @return the content disposition.
 */
public final String getContentDisposition() {
    return "attachment;filename=\"" + fileName + "\"";
}

/**
 * @return the relRoute
 */
public final String getRelRoute() {
    return relRoute;
}

/**
 * @param mRelRoute the relRoute to set
 */
public final void setRelRoute(final String mRelRoute) {
    this.relRoute = mRelRoute;
}

/**
 * @return the fileStreamToDownload
 */
public final InputStream getFileStreamToDownload() {
    return fileStreamToDownload;
}

/**
 * @param mFileStreamToDownload the fileStreamToDownload to set
 */
public final void setFileStreamToDownload(final InputStream mFileStreamToDownload) {
    this.fileStreamToDownload = mFileStreamToDownload;
}

/**
 * @return the fileName
 */
public final String getFileName() {
    return fileName;
}

/**
 * @param mFileName the fileName to set
 */
public final void setFileName(final String mFileName) {
    this.fileName = mFileName;
}

/**
 * @return the fileSize
 */
public final long getFileSize() {
    return fileSize;
}

/**
 * @param mFileSize the fileSize to set
 */
public final void setFileSize(final long mFileSize) {
    this.fileSize = mFileSize;
}

}

另一方面, struts.xml 文件中定义的操作:

<action name="downloadFile" class="org.test.action.FileDownloaderAction" method="execute">
    <result name="success" type="stream">
        <param name="contentType">application/octet-stream</param>
        <param name="inputName">fileStreamToDownload</param>
        <param name="contentDisposition">${contentDisposition}</param>
        <param name="contentLength">${fileSize}</param>
        <param name="bufferSize">4096</param>
        <param name="allowCaching">true</param>
        <param name="contentCharSet">UTF-8</param>
    </result>
    <result name="error">/error.jsp</result>
</action>

此代码基于我找到的一些示例。抛出异常时,Tomcat会打印出来:

abr 28, 2013 12:11:59 AM org.apache.catalina.core.StandardWrapperValve
invoke SEVERE: Servlet.service() para servlet default lanzó excepción java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:867)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:569)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:722)

有没有办法避免这种异常?

提前致谢。

1 个答案:

答案 0 :(得分:0)

将文件(在您的案例中为视频)数据写入响应时,浏览器中会出现

保存/取消对话框。浏览器检测到这是一个文件,并为用户提供文件保存对话框。当用户选择取消时,取消从浏览器到服务器的连接被取消,因此当服务器尝试将其余文件写入响应时,将记录此异常。我相信这种行为是有效的。由于取消操作完全发生在客户端(在浏览器中),因此您无法从服务器执行任何操作。