Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. private FTPClient ftp = null;
  2. @Value("${excelfile.location}")
  3. private String excelFileLocation;
  4. @Override
  5. public void uploadExcelToFTP() throws Exception {
  6. String host = "";
  7. String user = "";
  8. String pass = "";
  9. String hostDir = "";
  10. String localFileFullName = excelFileLocation+"errorSongs-"+dateToString(new Date())+".xlsx";
  11. String fileName = "errorSongs";
  12. ftp = new FTPClient();
  13. ftp.addProtocolCommandListener(new PrintCommandListener(
  14. new PrintWriter(System.out)));
  15. int reply;
  16. ftp.connect(host);
  17. reply = ftp.getReplyCode();
  18. if (!FTPReply.isPositiveCompletion(reply)) {
  19. ftp.disconnect();
  20. throw new Exception("Exception in connecting to FTP Server");
  21. }
  22. ftp.login(user, pass);
  23. ftp.setFileType(FTP.BINARY_FILE_TYPE);
  24. ftp.enterLocalPassiveMode();
  25.  
  26. File file = new File(localFileFullName);
  27. if (file.exists()) {
  28. try (InputStream input = new FileInputStream(file)) {
  29. this.ftp.storeFile(hostDir + fileName, input);
  30. }
  31. }
  32. /*else{
  33. throw new ExcelFileNotFoundException("File does not exist on location");
  34. }*/
  35. if (this.ftp.isConnected()) {
  36. try {
  37. this.ftp.logout();
  38. this.ftp.disconnect();
  39. } catch (IOException exception) {
  40. // do nothing as file is already saved to server
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement