Search code examples
javaexcelftpapache-commons-net

java ftp transferring file resulting in corrupted file


I'm new to this kind of work so please help me. I send a xlsx file (a excel file) to a server, there were no error in my code and either in the ftp server, I use filezilla in xampp. I search in google and say that it must be store in a zip file but the zip file is also corrupted. Here is my code

 FTPClient upload= new FTPClient();
   File firstLocalFile = new File("C:/Users/user/desktop/sample.xlsx");
   InputStream inputStream = new FileInputStream(firstLocalFile);
   try {

       upload.connect("localhost");
       upload.login("user", "pass");
       upload.enterLocalPassiveMode();
       upload.storeFile("sample.zip", inputStream);
   } finally {
       try {
           upload.logout();
           upload.disconnect();
       } catch (Exception e) {
       }
   }

Any solution for my problem?


Solution

  • You need to set the file type using -

    upload.setFileType(FTPClient.BINARY_FILE_TYPE);
    

    Also, add try and catch around upload.storeFile to make sure its storing the file.