Java/struts/servlet file Download and ftp file download

Source: Internet
Author: User
Tags ftp file ftp login server port

1. Front-end Code

Action or servlet using hyperlinks to struts

<a target= "_blank" href= "ftpfileaction!downloadfile.action? transurl=ftp://10.0.2.1/tazi/a.xml "> click Download </a>

2. Background Code Action or servlet

String S1=transurl;//Transurl is a parameter accepted by the foregroundS1=s1.substring (6); S1=s1.substring (S1.indexof ("/")); STRING fn=s1.substring (S1.lastindexof ("/") +1); String filepath=s1.substring (0,s1.lastindexof ("/")); Response.setcharacterencoding ("GBK"); Response.setcontenttype ("Application/octet-stream"); Response.AddHeader ("Content-disposition", "attachment;filename=\" "+ fn+" \ "");//The filename that is defined in the response file header contains the Chinese must be the original ISO-8859-1 encoding. String filename=NewString (Fn.getbytes ("iso-8859-1"), "GBK");//the file name to be used in the program must be converted to GBK encodingBooleanSuc=false; suc=downfilestream ("10.0.2.1", "MyUser", "MyPwd", filepath, filename, OS);//Downfilestream is a way to download FTP files to a file streamif(!suc)    {Response.reset (); Response.setcharacterencoding ("GBK"); PrintWriter writer=Newprintwriter (OS); Writer.write ("The specified file does not exist!"); Writer.close ();}Else{os.close ();}; 

3.ftp File Download processing

Note that the package introduced here is

org.apache.commons.net.ftp.*

(1). Download to Local

ImportOrg.apache.commons.net.ftp.FTP;Importorg.apache.commons.net.ftp.FTPClient;ImportOrg.apache.commons.net.ftp.FTPFile;Importorg.apache.commons.net.ftp.FTPReply; Public Static BooleanDownfile (String URL,//FTP server hostname        intPort//FTP Server PortString username,//FTP login AccountString Password,//FTP Login PasswordString RemotePath,//relative paths on the FTP serverString FileName,//the file name to downloadString LocalPath//save to Local path after download) {    BooleanSuccess =false; FtpClient FTP=Newftpclient (); Try {        intreply;        Ftp.connect (URL, port); //If you use the default port, you can connect directly to the FTP server using Ftp.connect (URL)Ftp.login (username, password);//LoginReply =Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply))            {Ftp.disconnect (); returnsuccess; } ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directoryftpfile[] fs =Ftp.listfiles (); Booleanftpfileexist=false;  for(Ftpfile ff:fs) {if(Ff.getname (). Equals (FileName)) {ftpfileexist=true; File dir=NewFile (LocalPath); if(!dir.exists ())                {Dir.mkdirs (); } File LocalFile=NewFile (LocalPath + file.separator +ff.getname ()); OutputStream is=NewFileOutputStream (LocalFile,true); Ftp.setbuffersize (1024); if(Ftp.setfiletype (Ftp.binary_file_type)) {Ftp.retrievefile (Ff.getname (), is);                Is.close ();        }}} ftp.logout (); if(ftpfileexist) {Success=true; }    } Catch(IOException e) {e.printstacktrace (); } finally {        if(ftp.isconnected ()) {Try{ftp.disconnect (); } Catch(IOException IoE) {}}} returnsuccess;}

(2). Download to file output stream OutputStream

 Public Static BooleanDownfilestream (String URL,//FTP server hostname        intPort//FTP Server PortString username,//FTP login AccountString Password,//FTP Login PasswordString RemotePath,//relative paths on the FTP serverString FileName,//the file name to downloadoutputstream OS) {    BooleanSuccess =false; FtpClient FTP=Newftpclient (); Try {        intreply;        Ftp.connect (URL, port); //If you use the default port, you can connect directly to the FTP server using Ftp.connect (URL)Ftp.login (username, password);//LoginReply =Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply))            {Ftp.disconnect (); return false; } ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directoryftpfile[] fs =Ftp.listfiles (); Booleanftpfileexist=false;  for(Ftpfile ff:fs) {String ffname=NewString (Ff.getname (). GetBytes ("Iso-8859-1"), "GBK"); if(Ffname.equals (fileName)) {ftpfileexist=true; Ftp.setbuffersize (1024); if(Ftp.setfiletype (Ftp.binary_file_type)) {Ftp.retrievefile (Ff.getname (), OS);        }}} ftp.logout (); if(ftpfileexist) {Success=true; }    } Catch(IOException e) {e.printstacktrace (); } finally {        if(ftp.isconnected ()) {Try{ftp.disconnect (); } Catch(IOException IoE) {}}} returnsuccess;}

Java/struts/servlet file Download and ftp file download

Related Article

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.