Using an FTP server is a good solution for applications that use files to Exchange data. This article uses Apache Jakarta Commons Net (Commons-net-3.3.jar) to implement the upload/download/delete of files on the FTP server based on the FileZilla server servers.
For detailed configuration procedures for FileZilla server servers, see the FileZilla Server Installation Configuration tutorial for details. Before a friend said, upload large files (more than hundreds of m of files) to the FTP server will reproduce the problem can not be renamed, but I personally test upload 2G files to FileZilla server does not have this problem, friends can rest assured that 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 Action/controller layer) * @param hostname ftp server address * @param portftp Server port number * @param usernameftp sign-in account * @param PASSWORDFTP login Password * @param pathnameftp Server save directory * @param filename file name after uploading to the FTP server * @param inputStream input file stream * @return */public static boolean uploadfile (string hostname, int port, string username, string Password, 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 FTP server Ftpclient.login (Username, password);// Whether the FTP server is successfully logged on Int replycode = ftpclient.getreplycode (); 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 Portftp Server port number * @param &nbSP;USERNAMEFTP login account * @param passwordftp login password * save directory for @param pathnameftp server * @param filename file name after uploading to the FTP server * @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;} /** * Uploading files (no renaming of files) * @param hostname ftp server address * @param Portftp Server port number * @param usernameftp login account * @param passwordftp Login Password * @param pathnameftp Server save directory * @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 originfilename) {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 portftp Server port number * @param usernameftp login account * @param passwordftp login Password * @pAram pathnameftp Server Save directory * @param filename file name to delete * @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 the FTP server Ftpclient.connect (hostname, port);//Login FTP server Ftpclient.login (Username, password);//Verify the FTP server is successfully logged in int replycode = Ftpclient.getreplycode (); if (! Ftpreply.ispositivecompletion (Replycode)) {Return flag;} Switch FTP directory ftpclient.changeworkingdirectory (pathname); Ftpclient.dele (filename); ftpclient.logout (); flag = true;} catch (exception e) {e.printstacktrace ();} finally{if (ftpclient.isconnected ()) {try {ftpclient.logout ();} catch (Ioexception e) {}}}return flag;} /** * download files * @param hOstname ftp server address * @param portftp Server port number * @param usernameftp login account * @param passwordftp login password * @param pathnameftp server file directory * @param FileName File name * @param localpath file path after download * @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 the FTP server Ftpclient.connect (hostname, port);//Login FTP server Ftpclient.login (Username, password);//Verify the FTP server is successfully logged in int replycode = Ftpclient.getreplycode (); if (! Ftpreply.ispositivecompletion (Replycode)) {Return flag;} Switch 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 ();} 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);}}
This article is from the "This person's IT World" blog, be sure to keep this source http://favccxx.blog.51cto.com/2890523/1677243
Java solution for uploading downloaded files via FTP server