gpt4 book ai didi

java - Spring MVC - 在内存中创建一个 ZIP 文件并让用户下载

转载 作者:行者123 更新时间:2023-11-29 06:05:53 25 4
gpt4 key购买 nike

我有一个网络项目

  1. 从 Oracle 数据库中提取数据
  2. 使用 HashMap 从该数据创建一个 XML 文件
  3. 将其压缩到内存中
  4. 让用户下载 ZIP 文件。

请注意,我不会在下载前创建物理文件。

我完成了 1-3。我似乎找不到下载部分的解决方案。我正在使用纯 Spring MVC(尽我所能)、Hibernate、MySQL。

家庭 Controller .java

@RequestMapping(value="/doretrieve", method=RequestMethod.POST, produces="application/zip")
@ResponseBody
public ZipOutputStream doRetrieve(@RequestParam(value="calcgrouplist") String selectedCalcGroups, @RequestParam(value="env") String currentEnv){

ZipOutputStream zipCalgGroups = null;
try {
String[] cgs = calcGroupService.insertToArray(selectedCalcGroups);

for(String cg:cgs){
System.out.println("Calculation Group: " + cg);
}

Map startRetrieve = calcGroupService.startRetrieve(currentEnv, cgs);

if (startRetrieve != null ){
zipCalgGroups = calcGroupService.zipCalcGroups(currentEnv, startRetrieve);
} else {
return null;
}
} catch (NullPointerException e) {
e.printStackTrace();
}
return zipCalgGroups;

}

计算组服务.java创建 xml 文件的 zip 文件的代码

public ZipOutputStream zipCalcGroups(String database, Map startRetrieve) {

//Sort
//SortCalcGroupParameters sort = new SortCalcGroupParameters();
//sort.run(new File("\\" + database));

Map<String, byte[]> mapXmlFiles = startRetrieve;

try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos))

{
for (Map.Entry<String, byte[]> mapXmlFile:mapXmlFiles.entrySet()){
ZipEntry entry = new ZipEntry(mapXmlFile.getKey());
zos.putNextEntry(entry);
zos.write(mapXmlFile.getValue());
zos.closeEntry();
}

return zos;
} catch (IOException e) {
e.printStackTrace();
}
return null;

最佳答案

我能够解决我自己的问题。以下是编辑后的方法:

家庭 Controller :

@RequestMapping(value="/doretrieve", method=RequestMethod.POST, produces="application/zip")
@ResponseBody
public byte[] doRetrieve(HttpServletResponse response, @RequestParam(value="calcgrouplist")
String selectedCalcGroups, @RequestParam(value="env") String currentEnv){

try {
String[] cgs = calcGroupService.insertToArray(selectedCalcGroups);

for(String cg:cgs){
System.out.println("Calculation Group: " + cg);
}

//returns map of file name and xml
Map startRetrieve = calcGroupService.startRetrieve(currentEnv, cgs);

//set file name of the zipped calc group/s using selected environment
response.setHeader("Content-Disposition", "attachment; filename=" + currentEnv + ".zip");

if (startRetrieve != null ){
byte[] zipped = calcGroupService.zipCalcGroupsFromMemory(currentEnv, startRetrieve);
return zipped;
} else {
return null;
}
} catch (NullPointerException e) {
e.printStackTrace();
}
return null;

}

计算组服务:

public byte[] zipCalcGroupsFromMemory(String database, Map startRetrieve) {

Map<String, byte[]> mapXmlFiles = startRetrieve;
HttpServletRequest request = null;

try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos)) {

for (Map.Entry<String, byte[]> mapXmlFile : mapXmlFiles.entrySet()) {

byte[] xml = sortCalcGroup(mapXmlFile.getKey(), mapXmlFile.getValue());

zos.putNextEntry(new ZipEntry(mapXmlFile.getKey()));
//zos.write(mapXmlFile.getValue());
zos.write(xml);
zos.closeEntry();
}
zos.close();
return baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

上面的代码生成了一个漂亮的压缩 xml 文件。

关于java - Spring MVC - 在内存中创建一个 ZIP 文件并让用户下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41713090/

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