Guest User

Untitled

a guest
May 8th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. public static void main(String[] args) {
  2. String server = "HOST";
  3. int port = 21;
  4. String user = "USER";
  5. String pass = "PASS";
  6.  
  7. FTPSClient ftpClient;
  8. try {
  9. ftpClient = new FTPSClient();
  10.  
  11. try {
  12.  
  13. ftpClient.connect(server, port);
  14. ftpClient.login(user, pass);
  15. ftpClient.enterLocalPassiveMode();
  16.  
  17. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  18.  
  19. // APPROACH #1: uploads first file using an InputStream
  20. File firstLocalFile = new File("TEST1.CSV");
  21.  
  22. String firstRemoteFile = "TEST1.txt";
  23. InputStream inputStream = new FileInputStream(firstLocalFile);
  24.  
  25. System.out.println("Start uploading first file");
  26.  
  27. boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
  28. inputStream.close();
  29. if (done) {
  30. System.out.println("The first file is uploaded successfully.");
  31. }
  32.  
  33. // APPROACH #2: uploads second file using an OutputStream
  34. File secondLocalFile = new File("TEST2.CSV");
  35. String secondRemoteFile = "TEST2.TXT";
  36. inputStream = new FileInputStream(secondLocalFile);
  37.  
  38. System.out.println("Start uploading second file");
  39. OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
  40. byte[] bytesIn = new byte[4096];
  41. int read = 0;
  42.  
  43. while ((read = inputStream.read(bytesIn)) != -1) {
  44. outputStream.write(bytesIn, 0, read);
  45. }
  46. inputStream.close();
  47. //outputStream.close();
  48.  
  49. boolean completed = ftpClient.completePendingCommand();
  50. if (completed) {
  51. System.out.println("The second file is uploaded successfully.");
  52. }
  53.  
  54. } catch (IOException ex) {
  55. System.out.println("Error: " + ex.getMessage());
  56. ex.printStackTrace();
  57. } finally {
  58. try {
  59. if (ftpClient.isConnected()) {
  60. ftpClient.logout();
  61. ftpClient.disconnect();
  62. }
  63. } catch (IOException ex) {
  64. ex.printStackTrace();
  65. }
  66. }
  67. } catch (NoSuchAlgorithmException e) {
  68. // TODO Auto-generated catch block
  69. e.printStackTrace();
  70. }
  71. }
Add Comment
Please, Sign In to add comment