I. Single File Upload
Develop action to implement file uploads
Package Cn.action;import Java.io.file;import Java.io.fileinputstream;import java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Org.apache.struts2.servletactioncontext;import Com.opensymphony.xwork2.actionsupport;public class Uploadaction extends actionsupport{//package upload file properties private files Upload The type of the encapsulated upload file private String uploadcontenttype; Package upload file name private String uploadfilename; The path of the encapsulated file upload private String savepath; Public String Execute () {byte[] buffer=new byte[1024]; try {fileinputstream fis=new FileInputStream (Getupload ()); FileOutputStream fos=new FileOutputStream (Getsavepath () + "\ \" +this.getuploadfilename ()); int length=fis.read (buffer); while (length>0) {fos.write (buffer, 0, length); Length=fis.read (buffer); } Fos.flush (); Fos.close (); Fis.close (); } catch (FileNotFoundException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } System.out.println ("========================"); return SUCCESS; } public File Getupload () {return upload; } public void Setupload (File upload) {this.upload = upload; } public String Getuploadcontenttype () {return uploadcontenttype; } public void Setuploadcontenttype (String uploadcontenttype) {this.uploadcontenttype = Uploadcontenttyp E } public String Getuploadfilename () {return uploadfilename; } public void Setuploadfilename (String uploadfilename) {this.uploadfilename = Uploadfilename; } public String GetsavepAth () {return Servletactioncontext.getservletcontext (). Getrealpath (Savepath); } public void Setsavepath (String savepath) {this.savepath = Savepath; } }
Encapsulates file information with three properties in action
The XXX property of the file type, like the Name property of the form's file control, encapsulates the file contents of the document control
The Xxxfilename property of the string type, which is combined by the preceding file type property and filename, is a fixed syntax, and is the file name that encapsulates the files of the document control
The Xxxcontenttype property of the string type, also composed of the XXX attribute and the contenttype, is a fixed syntax that encapsulates the file type of the file control counterpart
Configure action
<!--Single File upload- <action name= "upload" class= "cn.action.UploadAction" > <!-- Save directory path with param parameter settings-- <param name= "Savepath" >/upload</param> <result name= "Success" >success.jsp</result> </action>
:Two. multiple file uploadsSimply change the operation of a single file to a collection operation in the upload action.
Everything else is the same as a single upload.
Page
<!--uploading multiple files-- <action name= "someupload" class= "cn.action.SomeUploadAction" > <!-- Save directory path with param parameter settings-- <param name= "Savepath" >/upload</param> <result name= "Success" >success.jsp</result> </action>
Develop action to implement file uploads
Package Cn.action;import Java.io.file;import Java.io.fileinputstream;import java.io.fileoutputstream;import Org.apache.struts2.servletactioncontext;import Com.opensymphony.xwork2.actionsupport;public Class Someuploadaction extends Actionsupport {//package upload file properties private file[] upload; The type of the package upload file private string[] Uploadcontenttype; Package upload file name private string[] Uploadfilename; The path of the encapsulated file upload private String savepath; Public String Execute () throws exception{byte[] Buffer=new byte[1024]; for (int i = 0; i < upload.length; i++) {FileInputStream fis=new FileInputStream (getupload () [i]); FileOutputStream fos=new FileOutputStream (Getsavepath () + "\ \" +this.getuploadfilename () [i]); int length=fis.read (buffer); while (length>0) {fos.write (buffer, 0, length); Length=fis.read (buffer); } fos.flush (); Fos.close (); Fis.close (); } return SUCCESS; } public file[] Getupload () {return upload; } public void Setupload (file[] upload) {this.upload = upload; } public string[] Getuploadcontenttype () {return uploadcontenttype; } public void Setuploadcontenttype (string[] uploadcontenttype) {This.uploadcontenttype = Uploadcontentt Ype } public string[] Getuploadfilename () {return uploadfilename; } public void Setuploadfilename (string[] uploadfilename) {this.uploadfilename = Uploadfilename; } public String Getsavepath () {return Servletactioncontext.getservletcontext (). Getrealpath (sav Epath); } public void Setsavepath (String savepath) {this.savepath = Savepath; } }
Configure action
<!--uploading multiple files-- <action name= "someupload" class= "cn.action.SomeUploadAction" > <!-- Save directory path with param parameter settings-- <param name= "Savepath" >/upload</param> <result name= "Success" >success.jsp</result> </action>
Effect:Three. File downloadFile download requires InputStream, first in the file download action to provide a way to get inputstream, through the input stream can get the content of the file you want to download
Package Cn.action;import Java.io.bufferedinputstream;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.inputstream;import Org.apache.struts2.servletactioncontext;import Com.opensymphony.xwork2.actionsupport;public class Filedownaction extends Actionsupport {//Read the directory of the downloaded files private St Ring InputPath; File name of the download private String filename; Read the input stream of the downloaded file private InputStream InputStream; Type of download file private String Conetnttype; Public String Execute () {return SUCCESS; } public String Getinputpath () {return inputpath; } public void Setinputpath (String inputpath) {this.inputpath = InputPath; } public String GetFileName () {return fileName; The public void Setfilename (String fileName) {this.filename = FileName; } //create InputStream input stream public InputStream getInputStream () throws Exception {String Path=servletactioncontex T.getservletcontext (). Getrealpath (InputPath); Bufferedinputstream stream = new Bufferedinputstream (New FileInputStream (path+ "\ \" +filename)); return stream; } public void Setinputstream (InputStream inputstream) {this.inputstream = InputStream; } public String Getconetnttype () {return conetnttype; } public void Setconetnttype (String conetnttype) {this.conetnttype = Conetnttype; } }
Through the context to get the actual path of the downloaded file, build a InputStream input stream implementation file download read.
In the configuration file, the action is also configured, and the parameters of the stream result type are set.
<!--download specified action-- <action name= "Download" class= "cn.action.FileDownAction" > <param Name= "InputPath" >/upload</param> <result name= "Success" type= "Stream" > <param name= " ContentType ">application/octet-stream</param> <param name=" InputName ">inputstream</param > <param name= "contentdisposition" > attachment;filename= "${filename}" </param> <param name= "buffersize" >4096</param> </result> </action>
The contenttype parameter determines the type of download file, and the parameter values for different file types are different.
Typically, the contenttype parameter is set directly to Application/octet-stream.
The contentdisposition parameter consists of two parts, the previous part represents the form of the processing file, such as attachement indicates that the dialog box appears when downloading, the user saves or opens the file directly, and the latter part represents the file name of the downloaded file. Separate the two sections with ";".
Then develop a simple download page, set a hyperlink in the page, download the action via a hyperlink request
Effect:
Struts2 file upload and file download