gpt4 book ai didi

java - SFTP 传输/放置 (Jsch) 后 Zip 文件损坏(有点)

转载 作者:行者123 更新时间:2023-12-02 14:09:27 24 4
gpt4 key购买 nike

这是我用于通过 SFTP 发送文件的代码:

private void send (String sftpHost, int sftpPort, String user, String sshPrivateKeyPath, String sshPassphrase, String sftpDir, String fileName) {
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
FileInputStream fis = null;
try {
JSch jsch = new JSch();
jsch.addIdentity(sshPrivateKeyPath, sshPassphrase);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd(sftpDir);
File f = new File(fileName);
fis = new FileInputStream(f);
channelSftp.put(fis, f.getName(), ChannelSftp.OVERWRITE);
} catch (Exception ex) {
logger.error("sending file failed", ex);
throw new RuntimeException(ex);
}
finally{
try {
if(fis != null) fis.close();
} catch (IOException e) {
logger.error("Error closing stream", e);
}
if(channelSftp != null) channelSftp.exit();
if(channel != null) channel.disconnect();
if(session != null) session.disconnect();
}
}

两台机器都是CentOS 6.5 VMs, java 1.7.0_51, OpenJDK, tomcat 7.0.50

zip 使用 unzip -Z 在源/客户端服务器上运行。在目的地/服务器上,我收到此错误:

Archive:  filename.zip
[filename.zip]
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
zipinfo: cannot find zipfile directory in one of filename.zip or
filename.zip.zip, and cannot find filename.zip.ZIP, period.

文件大小也改变了:

来源(确定)

-rw-r--r--. 1 root root 49170 Oct 10 15:35 filename.zip

目的地(已损坏)

-rw-rw-r--. 1 user user 45710 Oct 10 15:35 filename.zip

我还尝试在损坏的文件上运行 jar -tf 并得到:

java.util.zip.ZipException: error in opening zip file

但是当我尝试 jar xvf 时,它成功地提取了文件。所以它并没有完全“损坏”

我也尝试通过 WinSCP 传输到我的 Windows 7 机器并尝试使用 7zip,但它也无法打开文件。

我在想也许 Jsch 有二进制文件的设置,但我没有找到。

感谢您提供的任何帮助/指导

更新 1:我也尝试了 Jsch 的 SCP 界面,结果相同

更新 2:我尝试了 sshj sftp,结果相同。所以不是 jsch 问题...

更新 3:我还尝试添加下面的代码。虽然文件大小改变了,但用 unzip -Z 还是打不开

config.put("compression.s2c", "none");
config.put("compression.c2s", "none");
config.put("compression_level", "0");

最佳答案

这都是我的愚蠢造成的。用于创建此代码使用的 zip 的 zipoutstream 尚未关闭,即 zip 创建和文件发送在同一个 try - catch - finally block 中。抱歉浪费了大家的时间。

关于java - SFTP 传输/放置 (Jsch) 后 Zip 文件损坏(有点),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26303706/

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