简体   繁体   English

Java FTP xls文件上传

[英]Java FTP xls file upload

I am trying to upload files to a FTP Server in a Java class. 我正在尝试将文件上传到Java类中的FTP服务器。 I use apache library: org.apache.commons.net.ftp.FTPClient. 我使用apache库:org.apache.commons.net.ftp.FTPClient。 The upload function works fine until I try to upload a XLS (Excel) file. 在我尝试上传XLS(Excel)文件之前,上传功能正常工作。 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. 非常感谢您的任何帮助。

I solved the problem using the suggestion in this thread: 我使用此线程中的建议解决了问题:

Transfer raw binary with apache commons-net FTPClient? 使用Apache Commons-Net FTPClient传输原始二进制文件?

All I needed to do was setting binary file mode for non .txt files: 我要做的就是为非.txt文件设置二进制文件模式:

if (fileExtension.equals("txt")) {
    ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);
} else {
    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM