Advertisement
Guest User

Ftp-demo.java

a guest
Sep 6th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. import java.io.BufferedOutputStream;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.net.SocketException;
  8.  
  9. import org.apache.commons.net.ftp.FTP;
  10. import org.apache.commons.net.ftp.FTPClient;
  11.  
  12. public class FileDownload {
  13.  
  14.     public static void main(String args[])
  15.     {
  16.         String server="ace.dev.demo";
  17.         String user="user1";
  18.         String pass="user1";
  19.         int port=21;
  20.  
  21.         FTPClient client = new FTPClient();
  22.         try {
  23.             client.connect(server,port);
  24.             client.login(user, pass);
  25.             client.enterLocalPassiveMode();
  26.             client.setFileType(FTP.BINARY_FILE_TYPE);
  27.  
  28.  
  29.              String remoteFile2 = "qwert.txt";
  30.                 File downloadFile2 = new File("D:/Downloads/d.txt");
  31.                 OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2));
  32.  
  33.                 InputStream inputStream = client.retrieveFileStream(remoteFile2);
  34.                 byte[] bytesArray = new byte[4096];
  35.                 int bytesRead = -1;
  36.  
  37.                 while ((bytesRead = inputStream.read(bytesArray)) != -1) {
  38.                     outputStream2.write(bytesArray, 0, bytesRead);
  39.                 }
  40.  
  41.                boolean success = client.completePendingCommand();
  42.                 if (success) {
  43.                     System.out.println("File #2 has been downloaded successfully.");
  44.                 }
  45.                 outputStream2.close();
  46.                 inputStream.close();
  47.  
  48.  
  49.         } catch (SocketException e) {
  50.             // TODO Auto-generated catch block
  51.             e.printStackTrace();
  52.         } catch (IOException e) {
  53.             // TODO Auto-generated catch block
  54.             e.printStackTrace();
  55.         }
  56.  
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement