2017-01-18 41 views
1

我有Spring MVC的 - 在內存中創建一個ZIP文件,然後讓用戶下載

  1. 從Oracle數據庫
  2. 提取數據的web項目中使用一個HashMap
  3. 創建一個XML文件中指出的數據將其壓縮到內存中
  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文件/秒

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; 

回答

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文件/秒。

相關問題