Struts2 File Download Error

Source: Internet
Author: User
  • Program error:

When learning the Struts framework, the file download section is implemented using the stream result type in struts. After the configuration is complete, run the program and report the following error:

HTTP status 500-can not find a java. Io. inputstream with the name [downfile] in the invocation stack. Check the <Param name = "inputname"> tag specified for this action.

  • Error analysis:

1. The action configuration in the Struts. xml configuration file is incorrect.

<action name="downloadfile" class="com.shengsiyuan.struts2.DownloadAction">    <result name="success" type="stream">     <param name="contentDisposition">attachment;filename="BeginLinuxProgramming.pdf"</param>    <param name="inputName">downFile</param>    </result><action>

Note: The parameters specified in inputname correspond to the get method of inputstream returned in the action.

public InputStream getDownloadFile(){}

The underlying principle is to automatically implement the get/Set Method Based on Struts's valuestack + reflection principle.

2. the specified file path does not exist.

3. The inputstream method returned in the get method is incorrect. the method itself may return null.

InputStream is=ServletActionContext.getServletContext().getResourceAsStream("D:\\BeginLinuxProgramming.pdf");

If you use the getservletcontext method, the online saying is that the file must be stored in the servletcontext, and you have no idea how to save it to the servletcontext:

File file = new File("D:\\BeginLinuxProgramming.pdf");InputStream fileIStream = null;try {fileIStream= new FileInputStream(file);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return fileIStream;


Struts2 File Download Error

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.