我有一堆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()」。
任何人都可以給我理由嗎?
我認爲它在這裏拋出:zos.putNextEntry(項) ;它在服務器內部很深處。沒有bowser特定的代碼。你能否檢查同一個請求不是同時發送兩次? – efekctive
@efekctive是的,當我從IE運行時,請求同時執行兩次。在鉻,它只是一次。爲什麼這樣?你能幫忙嗎? –
唯一發生在我身上的是識別你的請求/會話並在服務器上跟蹤它們。 – efekctive