Java Technology Implementation upload download file to FTP server (complete) _java

Source: Internet
Author: User
Tags ftp login ftp upload file rar filezilla server port

For more detailed information, please see below:

Using an FTP server is a good choice for applications that use files for interactive data. This article uses Apache Jakarta Commons Net (Commons-net-3.3.jar) to implement an upload/download/delete of files on an FTP server based on FileZilla server servers.

For more detailed configuration procedures for FileZilla server servers, see the FileZilla Server Installation configuration tutorial. Before a friend said, upload large files (more than hundreds of M files) to the FTP server will reproduce the problem can not be renamed, but I test upload 2G file to FileZilla server does not have the problem, friends can be assured to use the code.

Favftputil.java package com.favccxx.favsoft.util;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;

Import Java.io.OutputStream;
Import org.apache.commons.net.ftp.FTPClient;
Import Org.apache.commons.net.ftp.FTPFile;

Import org.apache.commons.net.ftp.FTPReply; public class Favftputil {/** * upload file (available for use by the Action/controller layer) * @param hostname FTP server address * @param port FTP Server port number * @ PARAM username FTP login account * @param password FTP login password * @param pathname FTP Server save directory * @param filename uploaded to the FTP server file name * @par Am InputStream input File stream * @return */public static Boolean UploadFile (string hostname, int port, string username, string p
 Assword, String pathname, String fileName, InputStream InputStream) {Boolean flag = false;
 FtpClient ftpclient = new FtpClient ();
 Ftpclient.setcontrolencoding ("UTF-8");
 try {//Connect FTP server Ftpclient.connect (hostname, port); Login to FTP server Ftpclient.login (username, password);
 Whether to successfully log on to the FTP server int replycode = Ftpclient.getreplycode (); if (!
 Ftpreply.ispositivecompletion (Replycode)) {return flag;
 } ftpclient.setfiletype (Ftpclient.binary_file_type);
 Ftpclient.makedirectory (pathname);
 Ftpclient.changeworkingdirectory (pathname);
 Ftpclient.storefile (FileName, InputStream);
 Inputstream.close ();
 Ftpclient.logout ();
 Flag = true;
 catch (Exception e) {e.printstacktrace ();
 } finally{if (ftpclient.isconnected ()) {try {ftpclient.disconnect ();
 catch (IOException e) {e.printstacktrace ();
 }} return flag; /** * Upload file (can rename file) * @param hostname FTP server address * @param port FTP Server port number * @param username FTP login account * @param pa  ssWOrd FTP Login Password * @param pathname FTP Server save directory * @param filename uploaded to the FTP server file name * @param originfilename the name of the file to be uploaded (absolute address) * @return */public static Boolean uploadfilefromproduction (string hostname, int port, string username, string password, String pathname, string filename, String originfilename) {Boolean flag = FalsE
 try {inputstream InputStream = new FileInputStream (new File (Originfilename)); 
 Flag = UploadFile (hostname, port, username, password, pathname, filename, inputstream);
 catch (Exception e) {e.printstacktrace ();
 return flag; /** * Upload file (can not do file rename operation) * @param hostname FTP server address * @param port FTP Server port number * @param username FTP login Account * @param Password FTP login password * @param pathname FTP Server save directory * @param originfilename the name of the file to be uploaded (absolute address) * @return/public static Boole An uploadfilefromproduction (string hostname, int port, string username, string password, string pathname, String Originfil
 ENAME) {Boolean flag = false;
 try {String fileName = new File (originfilename). GetName ();
 InputStream InputStream = new FileInputStream (new File (Originfilename)); 
 Flag = UploadFile (hostname, port, username, password, pathname, fileName, InputStream);
 catch (Exception e) {e.printstacktrace ();
 return flag;
 /** * Delete file * @param hostname FTP server address * @param port FTP Server port number* @param username FTP login account * @param password FTP login password * @param pathname FTP Server save directory * @param filename name of the file to be deleted * @return  */public static Boolean DeleteFile (string hostname, int port, string username, string password, string pathname, string
 FileName) {Boolean flag = false;
 FtpClient ftpclient = new FtpClient ();
 try {//Connect FTP server Ftpclient.connect (hostname, port);
 Login to FTP server Ftpclient.login (username, password);
 Verify that the FTP server is logged on successfully int replycode = Ftpclient.getreplycode (); if (!
 Ftpreply.ispositivecompletion (Replycode)) {return flag;
 }//Toggle FTP directory Ftpclient.changeworkingdirectory (pathname);
 Ftpclient.dele (filename);
 Ftpclient.logout ();
 Flag = true;
 catch (Exception e) {e.printstacktrace ();
 } finally{if (ftpclient.isconnected ()) {try {ftpclient.logout ();
 The catch (IOException e) {}}} return flag; /** * Download file * @param hostname FTP server address * @param port FTP Server port number * @param username FTP login account * @param password FTP logon secret Code * @param pathname FTP Server file directory * @param filename File name * @param localpath Download file path * @return/public static Boolean downloadFile (String hostname, int port,
 String Username, string password, string pathname, string filename, String localpath) {Boolean flag = false;
 FtpClient ftpclient = new FtpClient ();
 try {//Connect FTP server Ftpclient.connect (hostname, port);
 Login to FTP server Ftpclient.login (username, password);
 Verify that the FTP server is logged on successfully int replycode = Ftpclient.getreplycode (); if (!
 Ftpreply.ispositivecompletion (Replycode)) {return flag;
 }//Toggle FTP directory Ftpclient.changeworkingdirectory (pathname);
 ftpfile[] Ftpfiles = Ftpclient.listfiles ();  for (Ftpfile file:ftpfiles) {if (Filename.equalsignorecase (File.getname ())) {File LocalFile = new file (LocalPath + "/" +
 File.getname ());
 OutputStream OS = new FileOutputStream (localfile);
 Ftpclient.retrievefile (File.getname (), OS);
 Os.close ();
 } ftpclient.logout ();
 Flag = true;
 catch (Exception e) {e.printstacktrace (); } finally{if (ftpclient.isconnected ()) {try {ftpcliEnt.logout ();
 The catch (IOException e) {}}} return flag;

}} Favftputiltest.java package com.favccxx.favsoft.util;

Import Junit.framework.TestCase;
 public class Favftptest extends TestCase {public void Testfavftputil () {String hostname = "127.0.0.1";
 int port = 21;
 String username = "Business";
 String Password = "Business"; 
 String pathname = "Business/ebook"; 
 String filename = "Big.rar";
 String originfilename = "C:\\users\\downloads\\downloads.rar";
Favftputil.uploadfilefromproduction (hostname, port, username, password, pathname, filename, originfilename);
 
String localpath = "d:/";
 Favftputil.downloadfile (hostname, port, username, password, pathname, filename, localpath);
 }

}

Above is the Java through FTP upload file to download the complete code, I hope to help.

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.