0

I am developing a web application using struts2 .. In that I am downloding a file when user clicks a link .. When user clicks the link I am checking in my action class whether requested file is existed or not if it is existed then it is working fine ... WHen it is not existed I am giving a action error message .. But That page is redirecting to error page that i have mapped globally .. IN my console ther is no exception message except these error lines .. Can not find a java.io.InputStream with the name [fileInputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action. And I can see that action message in my error page ... I have debugged the problem also .. Mapping also perfect ..

<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
      <result name="success" type="stream">              
          <param name="contentType">application/octet-stream</param>
          <param name="inputName">fileInputStream</param>
          <param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
          <param name="bufferSize">1024</param>
       </result>

Is It possible to map back to the same JSP when The file is not existed in SYSTEM .. Thanks

4

1 回答 1

0

在action中检查文件是否存在,分别返回success& error。你可以这样做:

<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
      <result name="success" type="stream">              
          <param name="contentType">application/octet-stream</param>
          <param name="inputName">fileInputStream</param>
          <param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
          <param name="bufferSize">1024</param>
       </result>
<result name="error">/error.jsp</result>

错误.jsp

<s:actionerror/>
<s:actionmessage/>
or your hard-coded message

但是因为,您需要返回同一页面,所以我建议使用 ajax 调用下载操作并在成功时返回 200,在错误时返回 204,如下所示:

<action name="downloadAction" class="DownloadPDFAction" method="pdfDownload" >
          <result name="success" type="stream">              
              <param name="contentType">application/octet-stream</param>
              <param name="inputName">fileInputStream</param>
              <param name="contentDisposition">attachment;filename="${downloadDoc}".pdf</param>
              <param name="bufferSize">1024</param>
           </result>
    <result name="error" type="httpheader">
         <param name="status">204</param>
    </result>

ajax 调用可能类似于 -文档

$.ajax({
  statusCode: {
    404: function() {
      alert( "page not found" );
    },
    204: function() {
      alert( "file not found" );
    }
  }
});
于 2013-11-07T04:33:25.297 回答