Guest User

Untitled

a guest
May 30th, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.zip.CheckedInputStream;
  3. import java.util.zip.CheckedOutputStream;
  4. import java.util.zip.CRC32;
  5. import org.apache.commons.net.ftp.*;
  6.  
  7. public class Main implements java.io.Serializable
  8. {
  9. public static void main(String[] args) throws Exception
  10. {
  11. Main main = new Main();
  12. main.doTest();
  13. }
  14.  
  15. private void doTest() throws Exception
  16. {
  17. String host = "ftp.host.com";
  18. String user = "user";
  19. String pass = "pass";
  20.  
  21. String asciiDest = "/tmp/ascii";
  22. String binaryDest = "/tmp/binary";
  23.  
  24. String remotePath = "test/";
  25. String remoteFilename = "test.xml";
  26.  
  27. System.out.println("TEST.XML ASCII");
  28. MyFTPClient client = createFTPClient(host, user, pass, org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE);
  29. File path = new File("/tmp/ascii");
  30. downloadFTPFileToPath(client, "test/", "test.xml", path);
  31. System.out.println("");
  32.  
  33. System.out.println("TEST.XML BINARY");
  34. client = createFTPClient(host, user, pass, org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
  35. path = new File("/tmp/binary");
  36. downloadFTPFileToPath(client, "test/", "test.xml", path);
  37. System.out.println("");
  38.  
  39. System.out.println("TEST.MP3 ASCII");
  40. client = createFTPClient(host, user, pass, org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE);
  41. path = new File("/tmp/ascii");
  42. downloadFTPFileToPath(client, "test/", "test.mp3", path);
  43. System.out.println("");
  44.  
  45. System.out.println("TEST.MP3 BINARY");
  46. client = createFTPClient(host, user, pass, org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
  47. path = new File("/tmp/binary");
  48. downloadFTPFileToPath(client, "test/", "test.mp3", path);
  49. }
  50.  
  51. public static File downloadFTPFileToPath(MyFTPClient ftp, String remoteFileLocation, String remoteFileName, File path)
  52. throws Exception
  53. {
  54. // path to remote resource
  55. String remoteFilePath = remoteFileLocation + "/" + remoteFileName;
  56.  
  57. // create local result file object
  58. File resultFile = new File(path, remoteFileName);
  59.  
  60. // local file output stream
  61. CheckedOutputStream fout = new CheckedOutputStream(new FileOutputStream(resultFile), new CRC32());
  62.  
  63. // try to read data from remote server
  64. if (ftp.retrieveFile(remoteFilePath, fout)) {
  65. System.out.println("FileOut: " + fout.getChecksum().getValue());
  66. return resultFile;
  67. } else {
  68. throw new Exception("Failed to download file completely: " + remoteFilePath);
  69. }
  70. }
  71.  
  72. public static MyFTPClient createFTPClient(String url, String user, String pass, int type)
  73. throws Exception
  74. {
  75. MyFTPClient ftp = new MyFTPClient();
  76. ftp.connect(url);
  77. if (!ftp.setFileType( type )) {
  78. throw new Exception("Failed to set ftpClient object to BINARY_FILE_TYPE");
  79. }
  80.  
  81. // check for successful connection
  82. int reply = ftp.getReplyCode();
  83. if (!FTPReply.isPositiveCompletion(reply)) {
  84. ftp.disconnect();
  85. throw new Exception("Failed to connect properly to FTP");
  86. }
  87.  
  88. // attempt login
  89. if (!ftp.login(user, pass)) {
  90. String msg = "Failed to login to FTP";
  91. ftp.disconnect();
  92. throw new Exception(msg);
  93. }
  94.  
  95. // success! return connected MyFTPClient.
  96. return ftp;
  97. }
  98. }
Add Comment
Please, Sign In to add comment