Advertisement
Guest User

Untitled

a guest
Jan 27th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. public static void main(String[] args) {
  2. String server = "ftps-url";
  3. int port = 6321;
  4. String user = "";
  5. String pass = "";
  6.  
  7. FTPSClient ftp = null;
  8. try {
  9. ftp = new FTPSClient("SSL");
  10. ftp.setAuthValue("SSL");
  11. ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
  12.  
  13. int reply;
  14.  
  15. ftp.connect(server, port);
  16. System.out.println("Connecting");
  17. System.out.print(ftp.getReplyString());
  18.  
  19. // After connection attempt, you should check the reply code to verify success.
  20. reply = ftp.getReplyCode();
  21.  
  22. if (!FTPReply.isPositiveCompletion(reply)) {
  23. ftp.disconnect();
  24. System.err.println("FTP server refused connection.");
  25. System.exit(1);
  26. }
  27. ftp.login(user, pass);
  28.  
  29. ftp.execPBSZ(0);
  30. ftp.execPROT("P");
  31.  
  32. // ... // transfer files
  33. ftp.setBufferSize(1000);
  34. ftp.enterLocalPassiveMode();
  35. // ftp.setControlEncoding("GB2312");
  36. ftp.changeWorkingDirectory("/output"); //path where my files are
  37. ftp.setFileType(FTP.BINARY_FILE_TYPE);
  38. //System.out.println("Remote system is " + ftp.getSystemName());
  39.  
  40. String[] filelist = ftp.listNames(); //returns null
  41. System.out.println(filelist.length);
  42. System.out.println(Arrays.toString(filelist));
  43.  
  44. // method 1
  45. File inFile = new File("myfile.xls");
  46. if (!inFile.exists()) {
  47. inFile.createNewFile();
  48. }
  49. InputStream input = new FileInputStream(inFile);
  50. ftp.completePendingCommand();
  51. ftp.storeFile(filelist[0], input);
  52. input.close();
  53.  
  54. // method 2
  55. FileOutputStream fos = new FileOutputStream("myfile.xls");
  56. ftp.retrieveFile(filelist[0], fos);
  57. fos.flush();
  58. fos.close();
  59.  
  60. //ftp.completePendingCommand();
  61.  
  62. // method 3
  63. File path = new File("C:/Users/user/Desktop/");
  64. String remoteFilePath = "/output/" + filelist[0];
  65. File resultFile = new File(path, filelist[0]);
  66. CheckedOutputStream fout = new CheckedOutputStream(new FileOutputStream(resultFile), new CRC32());
  67. ftp.retrieveFile(remoteFilePath, fout);
  68. } catch (Exception ex) {
  69. System.out.println("error");
  70. ex.printStackTrace();
  71. } finally {
  72. // logs out and disconnects from server
  73. try {
  74. if (ftp.isConnected()) {
  75. ftp.logout();
  76. ftp.disconnect();
  77. }
  78. } catch (Exception ex) {
  79. ex.printStackTrace();
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement