First, the download example is not based on struts2.
Download () in the action class action, the Code is as follows: public String download () {try {FileInputStream FD = new FileInputStream (new File (path, newUser. getFilename (); // path indicates the file path newUser. setInputStreamxxx (FS ); /*************************************** **************************************** * ***************/HttpServletResponse response = ServletActionContext. getResponse (); response. setContentType ("application/x-msdownlo Ad "); response. setHeader ("Content-Disposition", "attachment; filename =" + newUser. getFilename (); */OutputStream OS = response. getOutputStream (); byte [] B = new byte [1024]; int len; while (len = FCM. read (B ))! =-1) {OS. write (B, 0, len);} OS. flush (); FCM. close ();} catch (FileNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} return "download ";}
Struts2-based download
* Download () in the action class action, the Code is as follows: public String download () {// path is the file storage path, newUser. getFilename is the File name of the corresponding File. FileInputStream FD = new FileInputStream (new File (path, newUser. getFilename (); // newUser is the Instance Object newUser of javabean. setInputStreamxxx (FS); return "download ";}
* In javabean, add an attribute: inputStream type. Used to download and obtain the file stream * in the struts. xml file, the configuration is as follows:
Application/x-msdownload
InputStreamxxx
Attachment; filename =$ {filename}
1024