当我尝试提取压缩文件时,文件已损坏。这是我用过的代码。我无法理解我的错误。文件的格式是.zip,里面有XML文件。从远程服务器下载后,XML文件标签会被更改并且看起来已损坏。
public Boolean pullConfirmationsFTP(String host, String sftpUserName, String sftpPwd,
String sftpPort, String fromConfirmationDirectory, String archiveConfirmationDirectory,
String toDirectory) {
try {
// new ftp client
FTPClient ftp = new FTPClient();
// try to connect
ftp.connect(host);
// login to server
if (!ftp.login(sftpUserName, sftpPwd)) {
ftp.logout();
LOG4J.error("Authentication failed");
}
int reply = ftp.getReplyCode();
// FTPReply stores a set of constants for FTP reply codes.
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
// enter passive mode
ftp.enterLocalPassiveMode();
// get system name
// System.out.println("Remote system is " + ftp.getSystemType());
// change current directory
ftp.changeWorkingDirectory(fromConfirmationDirectory);
System.out.println("Current directory is " + ftp.printWorkingDirectory());
// get list of filenames
FTPFile[] ftpFiles = ftp.listFiles();
if (ftpFiles != null && ftpFiles.length > 0) {
// loop thru files
for (FTPFile file : ftpFiles) {
try{
if (!file.isFile()) {
continue;
}
LOG4J.error("File is " + file.getName());
// get output stream
OutputStream output;
output = new FileOutputStream(toDirectory + file.getName());
// get the file from the remote system
ftp.retrieveFile(file.getName(), output);
// close output stream
output.close();
// delete the file
ftp.deleteFile(file.getName());
}
catch(Exception e)
{
LOG4J.error("Error in pushing file : ",e);
}
}
}
ftp.logout();
ftp.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
LOG4J.error(ex);
}
return true;
}
答案 0 :(得分:2)
您没有说出您使用的FTPClient,但我猜测它来自apache commons。 FTPClient的文档说:
FTPClient的默认设置是使用FTP.ASCII_FILE_TYPE,FTP.NON_PRINT_TEXT_FORMAT,FTP.STREAM_TRANSFER_MODE和FTP.FILE_STRUCTURE。直接支持的唯一文件类型是FTP.ASCII_FILE_TYPE和FTP.BINARY_FILE_TYPE
由于zip文件是二进制文件,您必须添加
JSON.strinfigy(data)