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.
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