开发者

Java FTP xls file upload

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 m开发者_如何学Cuch for any kind of help.


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜