【发布时间】:2017-02-14 13:17:00
【问题描述】:
我有一堆 Excel 表格,我想将其压缩并允许用户下载。对于压缩所有 excel 表,我使用了 java.util.zip.ZipOutputStream 库。
代码片段如下:
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();
}
这段代码运行在 Google 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 不会给出任何此类异常。帮助我弄清楚如何解决这个问题。是什么导致了这个问题。
我的浏览器版本: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();
}
所以由于按钮类型是提交并且在函数 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 中工作正常。 在 chrome 中,它也应该提交两次,因为按钮类型是提交,而且我还硬编码了“document.form.submit()”。
谁能告诉我原因?
【问题讨论】:
-
我假设它被扔在这里:zos.putNextEntry(entry);它在服务器的深处。没有特定于 Bowser 的代码。你能检查一下同一个请求没有同时发送两次吗?
-
@efekctive 是的,当我从 IE 运行时,请求同时进行两次。在 chrome 中,它只运行一次。为什么会这样?你能帮忙吗?
-
我唯一想到的就是识别您的请求/会话并在服务器上跟踪它们。
-
嗨@efekctive 我已经更新了我的问题。请参阅编辑部分。我的问题已解决,但如果您能提供帮助,我有一个后续问题。谢谢
-
另一种方式。发布问题。有人会捡起来的
标签: java google-chrome