Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.zip.CheckedInputStream;
- import java.util.zip.CheckedOutputStream;
- import java.util.zip.CRC32;
- import org.apache.commons.net.ftp.*;
- public class Main implements java.io.Serializable
- {
- public static void main(String[] args) throws Exception
- {
- Main main = new Main();
- main.doTest();
- }
- private void doTest() throws Exception
- {
- String host = "ftp.host.com";
- String user = "user";
- String pass = "pass";
- String asciiDest = "/tmp/ascii";
- String binaryDest = "/tmp/binary";
- String remotePath = "test/";
- String remoteFilename = "test.xml";
- System.out.println("TEST.XML ASCII");
- MyFTPClient client = createFTPClient(host, user, pass, org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE);
- File path = new File("/tmp/ascii");
- downloadFTPFileToPath(client, "test/", "test.xml", path);
- System.out.println("");
- System.out.println("TEST.XML BINARY");
- client = createFTPClient(host, user, pass, org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
- path = new File("/tmp/binary");
- downloadFTPFileToPath(client, "test/", "test.xml", path);
- System.out.println("");
- System.out.println("TEST.MP3 ASCII");
- client = createFTPClient(host, user, pass, org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE);
- path = new File("/tmp/ascii");
- downloadFTPFileToPath(client, "test/", "test.mp3", path);
- System.out.println("");
- System.out.println("TEST.MP3 BINARY");
- client = createFTPClient(host, user, pass, org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
- path = new File("/tmp/binary");
- downloadFTPFileToPath(client, "test/", "test.mp3", path);
- }
- public static File downloadFTPFileToPath(MyFTPClient ftp, String remoteFileLocation, String remoteFileName, File path)
- throws Exception
- {
- // path to remote resource
- String remoteFilePath = remoteFileLocation + "/" + remoteFileName;
- // create local result file object
- File resultFile = new File(path, remoteFileName);
- // local file output stream
- CheckedOutputStream fout = new CheckedOutputStream(new FileOutputStream(resultFile), new CRC32());
- // try to read data from remote server
- if (ftp.retrieveFile(remoteFilePath, fout)) {
- System.out.println("FileOut: " + fout.getChecksum().getValue());
- return resultFile;
- } else {
- throw new Exception("Failed to download file completely: " + remoteFilePath);
- }
- }
- public static MyFTPClient createFTPClient(String url, String user, String pass, int type)
- throws Exception
- {
- MyFTPClient ftp = new MyFTPClient();
- ftp.connect(url);
- if (!ftp.setFileType( type )) {
- throw new Exception("Failed to set ftpClient object to BINARY_FILE_TYPE");
- }
- // check for successful connection
- int reply = ftp.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- ftp.disconnect();
- throw new Exception("Failed to connect properly to FTP");
- }
- // attempt login
- if (!ftp.login(user, pass)) {
- String msg = "Failed to login to FTP";
- ftp.disconnect();
- throw new Exception(msg);
- }
- // success! return connected MyFTPClient.
- return ftp;
- }
- }
Add Comment
Please, Sign In to add comment