我正在使用 ByteArrayOutputStream 和 ZipOutputStream 生成一个 zip 文件。ZipEntry 中的文件名正常,编码正确。当它被称为“ByteArrayOutputStream.toByteArray()”并且正确创建了 zip 文件但在“cp866”处生成了 zipEntries 时,问题就出现了。在控制器处,返回类型是 ResponseEntity<byte[]>。
下面是代码的一部分。
byteArrayOutputStream = new ByteArrayOutputStream();
zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
zipOutputStream.putNextEntry(new ZipEntry("ação.pdf")); //Here it is all OK
zipOutputStream.write(inputStream.readAllBytes());
zipOutputStream.closeEntry();
返回 RestController 的代码类似于:
HttpHeaders headers = new HttpHeaders();
contentDisposition =
ContentDisposition.builder("attachment").filename("download.zip").build();
headers.setContentDisposition(contentDisposition);
headers.setContentLength(byteArrayOutputStream.size());
headers.setContentType(MediaType.parseMediaType("application/zip; charset-utf-8"));
return ResponseEntity.ok().headers(headers).body(byteArrayOutputStream.toByteArray());
在这种情况下,名为“ação.pdf”的文件会生成为“a├з├гo.pdf”,如果我在 ZipEntry 中指定字符集 ISO8859-1,它会生成为“aчуo.pdf”。
我试过的:
- 返回一个 StreamingResponseBody(而不是 byte[])
- 在 zipEntry 文件名字符串中指定字符集
- 在 zipEntry 对象中指定字符集
- 转换做base64(文件生成损坏)
没有所有可能的成功