The code is rough, here is only a record, easy to query later
/*** Upload file code*/ Public classUploadanddownloadactionextendsactionsupport{//Get File PrivateFile MyFile; //gets the name of the file PrivateString Myfilefilename; //get the type of file PrivateString Myfilecontenttype; PublicFile Getmyfile () {returnMyFile; } Public voidsetmyfile (File myFile) { This. MyFile =MyFile; } PublicString Getmyfilefilename () {returnMyfilefilename; } Public voidsetmyfilefilename (String myfilefilename) { This. Myfilefilename =Myfilefilename; } PublicString Getmyfilecontenttype () {returnMyfilecontenttype; } Public voidSetmyfilecontenttype (String myfilecontenttype) { This. Myfilecontenttype =Myfilecontenttype; } Public Booleanupload () {//1. Determine where the uploaded files are savedString Uploadrootpath = "/common/download/template"; String dir=struts2util.getrequest (). Getrealpath (Uploadrootpath); //2. Determine if this folder exists, there is no creationFile File =NewFile (dir); if(!file.exists ()) {File.mkdir (); } //3. Put the file in InputStream, and then OutputStream write the fileInputStream is =NULL; OutputStream OS=NULL; Try{ is=NewBufferedinputstream (NewFileInputStream (myFile)); OS=NewBufferedoutputstream (NewFileOutputStream (dir + "\ \" +myfilefilename)); byte[] buffer =New byte[1024]; intBytelen = 0; while((Bytelen = is.read (buffer)) > 0) {os.write (buffer,0, Bytelen); } domain.settemplatename (Myfilefilename); Domain.settemplateurl (Uploadrootpath+ "\\" +myfilefilename); Domain.settemplatefullpath (dir+ "\\" +myfilefilename); } Catch(Exception e) {}finally { if(Is! =NULL) { Try{is.close (); } Catch(IOException e) {}}if(OS! =NULL) { Try{os.close (); } Catch(IOException e) {}}} return true; }}
/*** File Download*/ Public classUploadanddownloadactionextendsActionsupport {PrivateString FileName; PublicString GetFileName () {Try{fileName= Urlencoder.encode (FileName, "UTF-8"); }Catch(Exception e) {Throw Newruntimeexception (); } returnFileName; } Public voidsetfilename (String fileName) { This. FileName =FileName; } //2.3: Method download for return stream PublicInputStream Getattrinputstream ()throwsitsexception, unsupportedencodingexception{String fileName=struts2util.getrequest (). GetParameter ("FileName"); This. Setfilename (FileName); InputStream is=servletactioncontext.getservletcontext (). getResourceAsStream ("Path"); returnis ; }Else{ return NULL; } } PublicString Getdownloadfile ()throwsitsexception {return"Success"; } /*** STRUTS2 Configuration*/<action name= "uploadanddownload*"class= "Uploadanddownloadaction" method= "{1}" > <result name= "uploadtemplate" >/common/download/jsp/uploadtemp late.jsp</result> <!--download-<result name= "Success" type= "Stream" > <param name= "ContentType" >application/octet-stream</param> <param name= "InputName" ;attrinputstream</param> <param name= "contentdisposition" >attachment;filename= "${fileName}" < ;/param> <param name= "buffersize" >1024</param> </result> &L T;/action>}
/*** Delete Files*/ Public Static BooleanDelete (String path) {File file=NewFile (path); if(!file.exists ()) { return false; }Else{ returnDeleteFile (path); }}/*** Delete Files*/ Public Static BooleanDeleteFile (String path) {File file=NewFile (path); //if the file path corresponds to a file that exists and is a file, it is deleted directly if(File.delete ()) {return true; }Else { return false; }}
Struts2 implement file Upload and download