java zip中的zip已破坏,上传到服务器时Zip文件被破坏

My java program uploads a zip file from my system to FTP server. uploadfile() is a function that contains the uploading code.

uploadfile("192.168.0.210","muruganp","vm4snk","/home/Admin/GATE521/LN_RB_Semivalid2junk/Output/"+date+"_RB1.zip","/fileserver/filesbackup/Emac/"+date+"_RB1.zip");

public static boolean uploadfile(String server, String username,

String Password, String source_file_path, String dest_dir) {

FTPClient ftp = new FTPClient();

try {

int reply;

ftp.connect(server);

ftp.login(username, Password);

System.out.println("Connected to " + server + ".");

System.out.print(ftp.getReplyString());

reply = ftp.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply)) {

ftp.disconnect();

System.err.println("FTP server refused connection.");

return false;

}

System.out.println("FTP server connected.");

InputStream input = new FileInputStream(source_file_path);

ftp.storeFile(dest_dir, input);

System.out.println(ftp.getReplyString());

input.close();

ftp.logout();

} catch (Exception e) {

System.out.println("err");

e.printStackTrace();

return false;

} finally {

if (ftp.isConnected()) {

try {

ftp.disconnect();

} catch (Exception ioe) {}

}

}

return true;

}

The zip file that I do have in my system is perfect. But after uploading the same in the server location,downloading the same, and extracting the problem occurs. "The file is corrupt" says the error. What should I do to resolve this issue. Kindly advise on this.

I suspect the problem would be something like, transferring through ASCII mode. It should actually be transferred through binary mode as per this QUESTION. How to attain the same? Please advise.

解决方案

Best guess is that the FTP upload is using ascii mode which will corrupt a binary file like a zip. Verify this and if so change it to binary mode instead.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用JavaZipOutputStream类将MultipartFile数组压缩为zip文件,然后再将zip文件上传到阿里云OSS。具体步骤如下: 1. 创建一个ZipOutputStream对象,并指定输出流为ByteArrayOutputStream。 2. 遍历MultipartFile数组,将每个文件的内容写入ZipOutputStream。 3. 将ZipOutputStream的内容转换为字节数组,并创建一个ByteArrayInputStream对象。 4. 将字节数组上传到阿里云OSS。 完整代码如下: ```java import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import org.springframework.web.multipart.MultipartFile; public class ZipAndUploadToOSSUtil { public static void zipAndUploadToOSS(MultipartFile[] files, String bucketName, String objectName, String endpoint, String accessKeyId, String accessKeySecret) throws IOException { // 创建一个ZipOutputStream对象,并指定输出流为ByteArrayOutputStream ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream); for (MultipartFile file : files) { // 获取MultipartFile文件名 String fileName = file.getOriginalFilename(); // 创建一个ZipEntry对象,表示zip文件的一个文件 ZipEntry zipEntry = new ZipEntry(fileName); zipOutputStream.putNextEntry(zipEntry); // 将MultipartFile的内容写入ZipOutputStream zipOutputStream.write(file.getBytes()); zipOutputStream.closeEntry(); } // 关闭ZipOutputStream zipOutputStream.close(); // 将ZipOutputStream的内容转换为字节数组 byte[] zipBytes = byteArrayOutputStream.toByteArray(); // 创建一个ByteArrayInputStream对象 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(zipBytes); // 创建OSSClient对象 OSS ossClient = new OSSClientBuilder().setEndpoint(endpoint).setAccessKeyId(accessKeyId).setAccessKeySecret(accessKeySecret).build(); // 上传字节数组到OSS ossClient.putObject(bucketName, objectName, byteArrayInputStream); // 关闭OSSClient和ByteArrayInputStream ossClient.shutdown(); byteArrayInputStream.close(); } } ``` 调用示例: ```java MultipartFile[] files = ... ; // MultipartFile数组 String bucketName = "yourBucketName"; String objectName = "yourObjectName.zip"; String endpoint = "yourEndpoint"; String accessKeyId = "yourAccessKeyId"; String accessKeySecret = "yourAccessKeySecret"; ZipAndUploadToOSSUtil.zipAndUploadToOSS(files, bucketName, objectName, endpoint, accessKeyId, accessKeySecret); ``` 其,`yourBucketName`为OSS存储桶的名称,`yourObjectName.zip`为上传到OSS的zip文件的对象名称,`yourEndpoint`为OSS的访问域名,`yourAccessKeyId`和`yourAccessKeySecret`为OSS的Access Key ID和Access Key Secret。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值