gpt4 book ai didi

java - ZipOutputStream + FTPClient 中的错误

转载 作者:行者123 更新时间:2023-12-01 14:23:26 25 4
gpt4 key购买 nike

我必须上传一个zip文件到ftp服务器,这里的zip文件也是动态构建的。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;

public class CommonsNet {
public static void main(String[] args) throws Exception {
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("127.0.0.1");
client.login("phani", "phani");
String filename = "D://junk.pdf";
fis = new FileInputStream(new File(filename));
byte[] bs = IOUtils.toByteArray(fis);
fis.close();
OutputStream outputStream = client.storeFileStream("remote.zip");

ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);

zipOutputStream.setLevel(ZipOutputStream.STORED);
addOneFileToZipArchive(zipOutputStream,
"junk.pdf", bs);
zipOutputStream.close();

outputStream.close();

client.logout();
System.out.println("Transfer done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void addOneFileToZipArchive(ZipOutputStream zipStream,
String fileName, byte[] content) throws Exception {
ZipArchiveEntry zipEntry = new ZipArchiveEntry(fileName);
zipStream.putNextEntry(zipEntry);
zipStream.write(content);
zipStream.flush();
zipStream.closeEntry();
}
}

执行此代码后,文件已成功创建,但我无法打开存档内的文件。像:

!   D:\phani\remote.zip: The archive is corrupt
! D:\phani\remote.zip: Checksum error in C:\Users\BHAVIR~1.KUM\AppData\Local\Temp\Rar$DIa0.489\MCReport.pdf. The file is corrupt

最佳答案

尝试在登录后添加 client.setFileType(FTP.BINARY_FILE_TYPE);

我记得默认传输模式是 ASCII,因此非 ASCII 文件可能会损坏。

关于java - ZipOutputStream + FTPClient 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17359816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com