Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private FTPClient ftp = null;
- @Value("${excelfile.location}")
- private String excelFileLocation;
- @Override
- public void uploadExcelToFTP() throws Exception {
- String host = "";
- String user = "";
- String pass = "";
- String hostDir = "";
- String localFileFullName = excelFileLocation+"errorSongs-"+dateToString(new Date())+".xlsx";
- String fileName = "errorSongs";
- ftp = new FTPClient();
- ftp.addProtocolCommandListener(new PrintCommandListener(
- new PrintWriter(System.out)));
- int reply;
- ftp.connect(host);
- reply = ftp.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- ftp.disconnect();
- throw new Exception("Exception in connecting to FTP Server");
- }
- ftp.login(user, pass);
- ftp.setFileType(FTP.BINARY_FILE_TYPE);
- ftp.enterLocalPassiveMode();
- File file = new File(localFileFullName);
- if (file.exists()) {
- try (InputStream input = new FileInputStream(file)) {
- this.ftp.storeFile(hostDir + fileName, input);
- }
- }
- /*else{
- throw new ExcelFileNotFoundException("File does not exist on location");
- }*/
- if (this.ftp.isConnected()) {
- try {
- this.ftp.logout();
- this.ftp.disconnect();
- } catch (IOException exception) {
- // do nothing as file is already saved to server
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement