gpt4 book ai didi

java - 多部分文件到 .zip

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

如何将 MultipartFile 转换为 .zip,然后转换为 byte?像这样的东西:

 MultipartFile file;
ZipInputStream zip = new ZipInputStream(file.getInputStream());
Base64.getEncoder().encode(zip)

最佳答案

MultipartFile 转换为 .zip 文件并检索其字节:

public ResponseEntity handleFile(@RequestParam MultipartFile file) throws IOException 
{
InputStream inputStream = file.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
ZipEntry zipEntry = new ZipEntry(file.getOriginalFilename());
zipOutputStream.putNextEntry(zipEntry);

byte[] bytes = new byte[1024];
int length;
while((length = inputStream.read(bytes)) >= 0) {
zipOutputStream.write(bytes, 0, length);
}
zipOutputStream.close();

// Do something with the byteArrayOutputStream
System.out.println(byteArrayOutputStream.toString());

return ResponseEntity.accepted().build();
}

关于java - 多部分文件到 .zip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54097549/

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