Transferred from: http://blog.csdn.net/hbcui1984/article/details/2720204
In Java programs, often need to deal with FTP, such as uploading files to the FTP server, download files, this article briefly describes how to use the Jakarta Commons in the FtpClient (in the commons-net package) implementation of the upload download file.
First, upload files
The principle is not introduced, we directly look at the code bar
[Java]View PlainCopy
- /**
- * Description: Uploading files to FTP server
- * @Version1.0 4:31:09 PM by Tri Hongbao ([email protected]) create
- * @param URL FTP server hostname
- * @param Port FTP Server
- * @param username FTP login Account
- * @param password FTP login password
- * Save Directory @param path FTP server
- * @param filename uploaded to the FTP server
- * @param input stream
- * @return returns TRUE, otherwise false
- */
- Public Static boolean uploadfile (string url,int port,string username, string password, string path, String fil ename, InputStream input) {
- Boolean success = false;
- ftpclient ftp = new FtpClient ();
- try {
- int reply;
- Ftp.connect (URL, port); //Connect FTP server
- //If using the default port, you can connect directly to the FTP server using Ftp.connect (URL)
- Ftp.login (username, password); //Login
- Reply = Ftp.getreplycode ();
- if (! Ftpreply.ispositivecompletion (Reply)) {
- Ftp.disconnect ();
- return success;
- }
- Ftp.changeworkingdirectory (path);
- Ftp.storefile (filename, input);
- Input.close ();
- Ftp.logout ();
- Success = true;
- } catch (IOException e) {
- E.printstacktrace ();
- } finally {
- if (ftp.isconnected ()) {
- try {
- Ftp.disconnect ();
- } catch (IOException IoE) {
- }
- }
- }
- return success;
- }<pre></pre>
Below we write two small examples:
1. Upload the local file to the FTP server with the following code:
[Java]View PlainCopy
- @Test
- Public void Testuploadfromdisk () {
- try {
- FileInputStream in=New FileInputStream (new File ("D:/test.txt"));
- Boolean flag = UploadFile ("127.0.0.1", + , "test", "test", "D:/ftp", "Test.txt", in);
- SYSTEM.OUT.PRINTLN (flag);
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- }
- }<pre></pre>
2. Generate a file on the FTP server and write a string to the file
[Java]View PlainCopy
- @Test
- Public void Testuploadfromstring () {
- try {
- InputStream input = new Bytearrayinputstream ("Test ftp". GetBytes ("Utf-8"));
- Boolean flag = UploadFile ("127.0.0.1", + , "test", "test", "D:/ftp", "test.txt", input);
- SYSTEM.OUT.PRINTLN (flag);
- } catch (Unsupportedencodingexception e) {
- E.printstacktrace ();
- }
- }<pre></pre>
Second, download the file
The code to download the file from the FTP server is also very simple, as follows:
[Java]View PlainCopy
- /**
- * Description: Download files from FTP server
- * @Version1.0 5:32:36 PM by Tri Hongbao ([email protected]) create
- * @param URL FTP server hostname
- * @param Port FTP Server
- * @param username FTP login Account
- * @param password FTP login password
- * @param the relative path on the RemotePath FTP server
- * @param filename to download
- * @param localPath saved to local path after download
- * @return
- */
- Public Static boolean downfile (string url, int port,string username, string password, string Remotepath,strin G filename,string LocalPath) {
- Boolean success = false;
- ftpclient ftp = new FtpClient ();
- try {
- int reply;
- Ftp.connect (URL, port);
- //If using the default port, you can connect directly to the FTP server using Ftp.connect (URL)
- Ftp.login (username, password); //Login
- Reply = Ftp.getreplycode ();
- if (! Ftpreply.ispositivecompletion (Reply)) {
- Ftp.disconnect ();
- return success;
- }
- 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 FileOutputStream (LocalFile);
- Ftp.retrievefile (Ff.getname (), is);
- Is.close ();
- }
- }
- Ftp.logout ();
- Success = true;
- } catch (IOException e) {
- E.printstacktrace ();
- } finally {
- if (ftp.isconnected ()) {
- try {
- Ftp.disconnect ();
- } catch (IOException IoE) {
- }
- }
- }
- return success;
- }<pre></pre>
Use FtpClient to upload downloads in Java