2017-02-11 79 views
0

我有一堆Excel工作表,我想压缩并使用户能够下载它。为了压缩所有excel表格,我使用了java.util.zip.ZipOutputStream库。下面java.util.zip.ZipException:重复项:动态Web项目Tomcat运行时异常

代码段中给出:

protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, 
      BindException errors) throws Exception { 
    // some code to get the list 
    Iterator<StudentSheet> it = list.iterator(); 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    ZipOutputStream zos = new ZipOutputStream(baos); 
    while (it.hasNext()) { 
     StudentSheet is = it.next(); 
     String fileName = is.getStudentCode() + "_" + is.getStudentName() + ".xls"; 
     fileName = fileName.replaceAll(" ", "_"); 
     logger.debug("FileName: " + fileName); 
     ZipEntry entry = new ZipEntry(fileName); 
     byte[] input =StudentManager.loadStudentSheetExcel(is.getId()); 
     entry.setSize(input.length); 
     zos.putNextEntry(entry); 
     zos.write(input); 
     zos.closeEntry(); 
     } 
    zos.close(); 
    String zipFileName = "ABC.zip"; 
    logger.debug("ZipFileName: " + zipFileName); 
    try { 
     streamZipOutput(zipFileName, "application/zip", baos, response); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
    finally { 
     baos.close(); 
    } 
    return null; 
} 



public void streamZipOutput(String zipFileName, String type, ByteArrayOutputStream baos, 
      HttpServletResponse response) throws IOException { 

     response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + "\""); 
     response.setContentType(type); 

     response.getOutputStream().write(baos.toByteArray()); 
     response.flushBuffer(); 

    } 

此代码运行从谷歌Chrome浏览器精绝。当我运行从Internet Explorer我的申请,我在运行期间得到这个异常:

Feb 11, 2017 5:45:00 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [action] in context with path [/appName] threw exception 
java.util.zip.ZipException: duplicate entry: 110_Vedant.xls 
    at java.util.zip.ZipOutputStream.putNextEntry(ZipOutputStream.java:232 

我无法弄清楚为什么重复条目的问题。 每个excel文件的名称都是唯一的,如果我生成同样的东西,chrome不会给出任何这样的异常。帮我弄清楚如何解决这个问题。什么导致了这个问题。

我的Internet Explorer版本:11.713.10586.0

编辑 早些时候我的提交按钮是这样的:

<div id="pageButtons"> 
    <button type="submit" onClick="sendAction('submit')"> Submit </button> 
</div> 

而且sendAction功能是这样的:

function sendAction(anAction){ 
    document.form._action.value = anAction ; 
    document.form.submit(); 
} 

因此,由于按钮类型是提交,也是我n函数sendAction我正在通过代码提交,所以我认为它正在同时提交两次。

所以我改变了按钮的HTML部分:

<div id="pageButtons"> 
     <button onClick="sendAction('submit')"> Submit </button> 
</div> 

它仍然是越来越提交两次。

然后我把它改为:

<div id="pageButtons"> 
     <button type="button" onClick="sendAction('submit')"> Submit </button> 
</div> 

然后在IE中运行良好。 现在我无法理解的是,如果type =“submit”的东西在IE中产生两次表单提交,为什么它在Chrome中工作正常。 铬也提交两次,因为按钮类型提交,我也硬编码“document.form.submit()”。

任何人都可以给我理由吗?

+0

我认为它在这里抛出:zos.putNextEntry(项) ;它在服务器内部很深处。没有bowser特定的代码。你能否检查同一个请求不是同时发送两次? – efekctive

+0

@efekctive是的,当我从IE运行时,请求同时执行两次。在铬,它只是一次。为什么这样?你能帮忙吗? –

+0

唯一发生在我身上的是识别你的请求/会话并在服务器上跟踪它们。 – efekctive

回答

0

我的提交按钮类型是type =“submit”,我也在调用一个javascript函数onClick这个提交按钮。在那个Javascript函数中,我写了document.form.submit(),所以表单被同时提交了两次。

<div id="pageButtons"> 
    <button type="submit" onClick="sendAction('submit')"> Submit </button> 
</div> 

而且sendAction功能是这样的:

function sendAction(anAction){ 
    document.form._action.value = anAction ; 
    document.form.submit(); 
} 

所以我改变了我的提交按钮类型:

<div id="pageButtons"> 
     <button type="button" onClick="sendAction('submit')"> Submit </button> 
</div> 
相关问题