Created
June 30, 2010 19:29
-
-
Save csuter/459103 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment