繁体   English   中英

受密码保护的zip文件问题

[英]password protected zip file issue

用户上传文件时,我需要用密码保护该文件,然后将其压缩到与运行我的代码的服务器不同的存储服务器中。 因此,我使用AESEncrypter加密文件,并使用jcraft.jsch.ChannelSftp将文件传输到服务器。

public ResponseEntity<ResponseWrapper> uploadFile(@RequestParam("uploads") MultipartFile file) throws Exception {
    FileOutputStream fos = new FileOutputStream("outputfile.zip");
    AESEncrypter aesEncrypter = new AESEncrypterBC();
    aze=new AesZipFileEncrypter(fos, aesEncrypter);
    aze.add(file.getOriginalFilename(), file.getInputStream(), "test123");

    JSch ssh = new JSch();
    Session session = ssh.getSession("username", "Servername", 22);

    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.setPassword("*****");
    session.connect();
    Channel channel = session.openChannel("sftp");
    channel.connect();

    sftp = (ChannelSftp) channel;
    sftpChannel.put(file.getInputStream(), "/storedfiles/outputfile.zip");
}

文件正在传输到服务器,但是当我下载该传输的文件并尝试打开它时,显示“发现错误打开文件“ ..”,您无法解压缩文件。是否要解决问题”。 不知道为什么我遇到这个问题,它还在本地服务器上创建文件,是哪行引起的?

我尝试替换此行

aze=new AesZipFileEncrypter(fos, aesEncrypter);

aze=new AesZipFileEncrypter("outputfile.zip", aesEncrypter); 

但努力工作。

我将文件放在远程服务器上,在输出流中读取该文件,然后用密码保护,解决了我的问题。

public ResponseEntity<ResponseWrapper> uploadFile(@RequestParam("uploads") MultipartFile file) throws Exception {
JSch ssh = new JSch();
Session session = ssh.getSession("username", "Servername", 22);

config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword("*****");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();

sftp = (ChannelSftp) channel;
OutputStream os = sftp.put("/storedfiles/outputfile.zip");

AESEncrypter aesEncrypter = new AESEncrypterBC();
aze=new AesZipFileEncrypter(os, aesEncrypter);
aze.add(file.getOriginalFilename(), file.getInputStream(), "test123");
if(aze != null) {
 aze.close();
}    
}

暂无
暂无

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

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