Struts2 implement file Upload and download

Source: Internet
Author: User

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}" &lt ;/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

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.