STRUTS2 File Download error hint solution

Source: Internet
Author: User

1. JSP page Hyperlink <a href= "javascript:void (0)" onclick= "DownloadFile (' ${id} ')" >${name}</a>

2. Form submission

<form action= "downloadfile.html" id= "Downloadfileform" "method=" Post "enctype=" Multipart/form-data "target=" Downloadfileiframe ">
<input type= "hidden" id= "Downloadfileid" name= "id"/>
<iframe name= "Downloadfileiframe" style= "Display:none" ></iframe>
</form>
<script type= "Text/javascript" >
function DownloadFile (ID) {
$ ("#downloadFileId"). Val (id);
Downloadfileform.submit ();
}
</SCRIPT>

3. Struts2.xml

<action name= "DownloadFile" class= "cn.shaviation.frm.webapp.action.FileUploadAction" method= "Download" >
<result name= "Success" type= "Stream" >
<param name= "ContentType" >${sysfile.type}</param>
<param name= "InputName" >inputStream</param>
<param name= "BufferSize" >40960</param>
</result>
<result name= "Input" >/WEB-INF/pages/common/downloadFileErrorMessage.jsp</result>
</action>

4. The download method in Fileuploadaction

Public String Download () {//download file
try {
Sysfile = Sysfilemanager.get (ID);
InputStream = new FileInputStream (Sysfile.getpath ());
GetResponse (). SetHeader ("Content-disposition", "attachment; Filename=" + Urlencoder.encode (Sysfile.getName (), "      UTF-8 "). ReplaceAll ("//+ ","%20 ")); ReplaceAll ("//+", "%20") the file name used to process spaces
catch (FileNotFoundException e) {
Downloadfilemessage= "The file does not exist on the server";
return INPUT;
catch (Unsupportedencodingexception e) {
Downloadfilemessage= "Other errors";
return INPUT;
}

return SUCCESS;
}

5. Error handling of Download

 <script language= "javascript" >    
       var Downloadfilemessage = "<%=request.getattribute (" Downloadfilemessage ")%>";
       if (downloadfilemessage!= "null") {
              alert (downloadfilemessage);
      }
 </script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.