Advertisement
Guest User

Untitled

a guest
Aug 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. public void upload(String domain, String user1, String password, String path, String fileName) {
  2. String server = domain;
  3. int port = 21;
  4. String user = user1;
  5. String pass = password;
  6.  
  7. FTPClient ftpClient = new FTPClient();
  8. try {
  9.  
  10. try {
  11. ftpClient.connect(server, port);
  12. ftpClient.login(user, pass);
  13. ftpClient.enterLocalPassiveMode();
  14.  
  15. } catch (Exception e) {
  16. }
  17. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  18.  
  19. // APPROACH #1: uploads first file using an InputStream
  20. File firstLocalFile = new File(path);
  21.  
  22. String firstRemoteFile = fileName;
  23. InputStream inputStream = new FileInputStream(firstLocalFile);
  24.  
  25. System.out.println("Start uploading first file");
  26. boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
  27. inputStream.close();
  28. if (done) {
  29. System.out.println("The first file is uploaded successfully.");
  30. }
  31.  
  32. } catch (IOException ex) {
  33. System.out.println("Error: " + ex.getMessage());
  34. ex.printStackTrace();
  35. } finally {
  36. try {
  37. if (ftpClient.isConnected()) {
  38. ftpClient.logout();
  39. ftpClient.disconnect();
  40. }
  41. } catch (IOException ex) {
  42. ex.printStackTrace();
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement