2

I am trying to upload files to a FTP Server in a Java class. I use apache library: org.apache.commons.net.ftp.FTPClient. The upload function works fine until I try to upload a XLS (Excel) file. In particular, when I upload it, the file is uploaded, but it seems to be corrupted. In fact its size is different from the original size and when I try to open it, it doesn't open correctly and doesn't show all the data.

Here is a portion from the code I use:

FTPClient ftpClient = new FTPClient();
File[] fileList;fileList = localFilePath.listFiles();
for (File file : fileList) {
    String fileName = file.getName();
    FileInputStream fileInputStream = new FileInputStream(file);
    ftpClient.storeFile(fileName, fileInputStream);
    fileInputStream.close();
}

Thank you very much for any kind of help.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Enrico
  • 103
  • 2
  • 10

1 Answers1

4

I solved the problem using the suggestion in this thread:

Transfer raw binary with apache commons-net FTPClient?

All I needed to do was setting binary file mode for non .txt files:

if (fileExtension.equals("txt")) {
    ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);
} else {
    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
}
Community
  • 1
  • 1
Enrico
  • 103
  • 2
  • 10
  • Thank you very much! Also, as a side note, you can access those constant directly from the class FTP instead of FtpClient... – spauny Feb 06 '13 at 13:44