java 如何将字节数组转换为 zip 文件并在浏览器中下载
How to convert a byte array to zip file and downloaded in the browser in java
我需要编写代码将字节数组转换为 zip 文件并使其在 spring MVC 中下载。字节数组来自 web 服务,它最初是一个 zip 文件。 Zip 文件有一个文件夹,该文件夹包含 2 个文件。我编写了以下代码以将字节数组转换为 zip 输入流。但我无法转换成 zip 文件。请帮助我。这是我的代码。
ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(bytes));
ZipEntry entry = null;
while ((entry = zipStream.getNextEntry()) != null) {
String entryName = entry.getName();
FileOutputStream out = new FileOutputStream(entryName);
byte[] byteBuff = new byte[4096];
int bytesRead = 0;
while ((bytesRead = zipStream.read(byteBuff)) != -1)
{
out.write(byteBuff, 0, bytesRead);
}
out.close();
zipStream.closeEntry();
}
zipStream.close();
我假设您要将字节数组写入 zip 文件。由于发送的数据也是一个 zip 文件并且要保存的也是 zip 文件,所以应该没有问题。
2 个步骤,将其保存在磁盘上并 return 文件。
保存在磁盘部分:
File file = new File(/path/to/directory/save.zip);
if (file.exists() && file.isDirectory()) {
try {
OutputStream outputStream = new FileOutputStream(new File(/path/to/directory/save.zip));
outputStream.write(bytes);
outputStream.close();
} catch (IOException ignored) {
}
} else { // create directory and call same code }
}
现在要取回并下载它,您需要一个控制器:
@RequestMapping(value = "/download/attachment/", method = RequestMethod.GET)
public void getAttachmentFromDatabase(HttpServletResponse response) {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getFileName() + "\"");
response.setContentLength(file.length);
FileCopyUtils.copy(file as byte-array, response.getOutputStream());
response.flushBuffer();
}
我已经编辑了我的代码,因此您必须进行一些更改才能 100% 适合您。让我知道这是否是您要找的。如果没有,我会删除我的答案。享受吧。
我需要编写代码将字节数组转换为 zip 文件并使其在 spring MVC 中下载。字节数组来自 web 服务,它最初是一个 zip 文件。 Zip 文件有一个文件夹,该文件夹包含 2 个文件。我编写了以下代码以将字节数组转换为 zip 输入流。但我无法转换成 zip 文件。请帮助我。这是我的代码。
ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(bytes));
ZipEntry entry = null;
while ((entry = zipStream.getNextEntry()) != null) {
String entryName = entry.getName();
FileOutputStream out = new FileOutputStream(entryName);
byte[] byteBuff = new byte[4096];
int bytesRead = 0;
while ((bytesRead = zipStream.read(byteBuff)) != -1)
{
out.write(byteBuff, 0, bytesRead);
}
out.close();
zipStream.closeEntry();
}
zipStream.close();
我假设您要将字节数组写入 zip 文件。由于发送的数据也是一个 zip 文件并且要保存的也是 zip 文件,所以应该没有问题。 2 个步骤,将其保存在磁盘上并 return 文件。
保存在磁盘部分:
File file = new File(/path/to/directory/save.zip);
if (file.exists() && file.isDirectory()) {
try {
OutputStream outputStream = new FileOutputStream(new File(/path/to/directory/save.zip));
outputStream.write(bytes);
outputStream.close();
} catch (IOException ignored) {
}
} else { // create directory and call same code }
}
现在要取回并下载它,您需要一个控制器:
@RequestMapping(value = "/download/attachment/", method = RequestMethod.GET)
public void getAttachmentFromDatabase(HttpServletResponse response) {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getFileName() + "\"");
response.setContentLength(file.length);
FileCopyUtils.copy(file as byte-array, response.getOutputStream());
response.flushBuffer();
}
我已经编辑了我的代码,因此您必须进行一些更改才能 100% 适合您。让我知道这是否是您要找的。如果没有,我会删除我的答案。享受吧。