Use component implementations in spring to upload/download files from an FTP server

Source: Internet
Author: User
Tags create directory ftp login ftp server path

Ftputil.java

Import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.OutputStream;import Org.apache.commons.net.ftp.ftp;import Org.apache.commons.net.ftp.ftpclient;import Org.apache.commons.net.ftp.ftpfile;import org.apache.commons.net.ftp.ftpreply;/** * FTP upload Download Tool class * <p>Title: ftputil</p> * <p>description: </p> * @version 1.0 */public class Ftputil {/** * Description: Uploading a text to an FTP server * @param host FTP server hostname * @param port FTP server ports * @param username FTP login account * @param password FTP login password * @param ba Sepath FTP Server Base directory * @param filePath FTP server file storage path. For example, the date of storage:/2015/01/01. The path to the file is Basepath+filepath * @param filename is uploaded to the file name on the FTP server * @param input stream * @return successfully returns TRUE, otherwise false */public STA Tic Boolean UploadFile (string host, int port, string username, string password, string basepath,string FilePath, String fi Lename, InputStream Input) {Boolean resUlt = false; ftpclient ftp = new ftpclient (), try {int Reply;ftp.connect (host, port)///Connect FTP server//If using default port, you can use Ftp.connect (host) Direct connection to the FTP server Ftp.login (username, password);//Login reply = Ftp.getreplycode (); Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect (); return result;} Switch to upload directory if (!ftp.changeworkingdirectory (Basepath+filepath)) {//If directory does not exist create directory string[] dirs = Filepath.split ("/"); String TempPath = Basepath;for (string dir:dirs) {if (null = = Dir | | ". Equals (dir)) Continue;temppath + ="/"+ dir;if (!ftp.changeworkingdirectory (TempPath)) {if (!ftp.makedirectory ( TempPath) {return result;} else {ftp.changeworkingdirectory (TempPath);}}} Set the type of upload file to binary type Ftp.setfiletype (ftp.binary_file_type);//Upload file if (!ftp.storefile (filename, input)) {return result;} Input.close (); ftp.logout (); result = true;} catch (IOException e) {e.printstacktrace ();} finally {if (ftp.isconnected ()) {try {ftp.disconnect ();} catch (IOException IOE) {}}}return result;} /** * Description: Download files from FTP server * @param host FTP Server hostname * @param port FTP server ports * @param username FTP login account * @param password FTP login password * @param relative to the RemotePath FTP server Path * @param filename to download * @param localPath the path saved to local after download * @return */public static Boolean downloadFile (String ho St, int port, string username, string password, string remotepath,string fileName, String localPath) {Boolean result = Fal Se ftpclient ftp = new ftpclient (), try {int Reply;ftp.connect (host, port), or//if using the default port, you can use Ftp.connect (host) Direct connection to the FTP server Ftp.login (username, password);//Login reply = Ftp.getreplycode (); Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect (); return result;} Ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directory ftpfile[] fs = Ftp.listfiles (); for (Ftpfile Ff:fs) {if ( Ff.getname (). Equals (fileName) {file LocalFile = new File (LocalPath + "/" + ff.getname ()); OutputStream is = new FileOutput Stream (LocalFile); Ftp.retrievefile (Ff.getname (), is); Is.close ();}} Ftp.logout (); result = true;} catch (IOException e) {e.printstacktrace ();} finAlly {if (ftp.isconnected ()) {try {ftp.disconnect ();} catch (IOException IoE) {}}}return result;} public static void Main (string[] args) {try {FileInputStream in=new fileinputstream (New File ("D:\\temp\\image\\g          Aigeming.jpg ")); Boolean flag = UploadFile ("192.168.25.133", +, "Ftpuser", "Ftpuser", "/home/ftpuser/www/images", "/2015/01/21", "          Gaigeming.jpg ", in);      SYSTEM.OUT.PRINTLN (flag);      } catch (FileNotFoundException e) {e.printstacktrace (); }  }}

Use component implementations in spring to upload/download files from an FTP server

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.