간단한 Struts2 클래스를 코딩하여 비디오 파일을 다운로드하려고합니다. java.lang.IllegalStateException
이 발생했기 때문에 다운로드 대화 상자에서 다운로드를 취소 할 때를 제외하고는 제대로 작동합니다 (페이지가 제대로 작동하고 예외가 기록됨). 의 struts.xml 파일에 정의 된 작업을파일 다운로드 대화 상자의 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;
}
} 한편
:
이<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)
이 예외를 피할 수있는 방법이 있습니까? 사전에
감사합니다.
취소 버튼을 눌렀을 때 어떤 서버 쪽 요청을 보내시겠습니까? –
나는 믿지 않는다. @ dev-blanked가 작성한대로 Cancel을 클릭하면 클라이언트 측에서 발생하지만 서버는 계속 데이터를 보내므로 예외가 발생합니다. 또한, [Stream Result page] (http://struts.apache.org/release/2.0.x/docs/stream-result.html)는 많은 도움이되지 않습니다 ... – dcordonu
@dcordonu 출력을 막을 수 있습니다. 오류를 보내지 않도록 발송자에게 알리는 것입니다. 어쨌든 예외 인터셉터를 통해 예외를 처리 할 수 있습니다. http://stackoverflow.com/questions/16213900/there-is-no-action-mapped-for-action-name-index-how-to-catch-the-exception-bec/16214532#16214532 –