2014-10-10 3 views
1

이것은 내가 SFTP를 통해 파일을 전송하기 위해 사용하는 코드 :Zip 파일 손상 (종류) 후 SFTP 전송/넣어 (Jsch)

두 시스템
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 VM에있는 자바 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 

Detination (손상)

-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는 바이너리 파일에 대한 설정을 가지고 있지만 필자는 아무 것도 발견하지 못했습니다. 어떤 도움도/방향

덕분에 당신은

UPDATE 1을 제공 할 수 있습니다 : 나는 또한 동일한 결과

UPDATE 2 Jsch의 SCP 인터페이스를 시도했다 : 나는 sshj 시도했다 SFTP를 동일한 결과와 함께. 그래서 jsch 문제가 아니 ...

업데이트 3 : 나는 또한 아래 코드를 추가하려고했습니다. 파일 크기가 변경되었지만 압축 해제시 열리지 않습니다. -Z

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

[ZIP 사양] (http://www.pkware.com/documents/casestudies/APPNOTE.TXT)입니다. 파일이 잘 렸습니다. 전송 전후의 길이를 비교하십시오. –

+0

고마워, 왜자를거야? – patlv23

+0

Dunno - 나는 파일 전송 이전에 데이터를 잘라 버리는 것에 대해 들어 본 적이 없다. –

답변

0

이것은 모두 어리 석음으로 인한 것입니다. 이 코드에서 사용하는 우편 번호를 생성하는 데 사용 된 우편 스트림이 아직 닫히지 않았습니다. 즉 우편 생성 및 파일 전송이 동일한 try-catch-finally 블록에 있습니다. 아무도 시간 낭비해서 죄송합니다.