Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.SocketException;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPClient;
- public class FileDownload {
- public static void main(String args[])
- {
- String server="ace.dev.demo";
- String user="user1";
- String pass="user1";
- int port=21;
- FTPClient client = new FTPClient();
- try {
- client.connect(server,port);
- client.login(user, pass);
- client.enterLocalPassiveMode();
- client.setFileType(FTP.BINARY_FILE_TYPE);
- String remoteFile2 = "qwert.txt";
- File downloadFile2 = new File("D:/Downloads/d.txt");
- OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2));
- InputStream inputStream = client.retrieveFileStream(remoteFile2);
- byte[] bytesArray = new byte[4096];
- int bytesRead = -1;
- while ((bytesRead = inputStream.read(bytesArray)) != -1) {
- outputStream2.write(bytesArray, 0, bytesRead);
- }
- boolean success = client.completePendingCommand();
- if (success) {
- System.out.println("File #2 has been downloaded successfully.");
- }
- outputStream2.close();
- inputStream.close();
- } catch (SocketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement