【问题标题】:Spring MVC - Create a ZIP file in memory and let user downloadSpring MVC - 在内存中创建一个 ZIP 文件并让用户下载
【发布时间】:2017-06-02 10:59:27
【问题描述】:

我有一个网络项目

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

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

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

HomeController.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;

    }

CalcGroupService.java 创建xml文件的zip文件的代码/s

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;

【问题讨论】:

    标签: java mysql hibernate spring-mvc


    【解决方案1】:

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

    HomeController:

    @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;
    
    }
    

    CalcGroupService:

    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 压缩文件。

    【讨论】:

      猜你喜欢
      • 2014-06-30
      • 2014-11-07
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 2011-01-16
      相关资源
      最近更新 更多