Java 多部分文件到.zip
如何将多部分文件转换为.zip,然后再转换为字节? 比如:Java 多部分文件到.zip,java,spring,Java,Spring,如何将多部分文件转换为.zip,然后再转换为字节? 比如: MultipartFile file; ZipInputStream zip = new ZipInputStream(file.getInputStream()); Base64.getEncoder().encode(zip) 将MultipartFile转换为.zip文件并检索其字节: public ResponseEntity handleFile(@RequestParam MultipartFile file) th
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();
}
为了什么?您是接收文件并以这种方式存储,还是从磁盘读取文件并将其作为响应提供。我想以字节形式存储在数据库中。纠正问题你可能想看看这个