wood have a look at this great God how to solve it ?
The following my code :
js:
$("#btnExport").click(function(event) {
$("#op").val("export");
$("#pageNo").val(1);
$("#frmQuery").submit();
$("#op").val("");
});
action:
protected void exportFile(File file) {
logger.debug("file=" + file);
ServletOutputStream out = null;
try {
byte[] fileData = FileUtils.readFileToByteArray(file);
String filename = file.getName();
logger.debug("fileData.length=" + fileData.length);
HttpServletResponse response = ServletActionContext.getResponse();
response.addHeader("Content-Disposition", "attachment; filename="
+ filename);
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream");
out = response.getOutputStream();
out.write(fileData);
} catch (Exception e) {
logger.error("输出文件时出错! " + file.getAbsolutePath(), e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
------ Solution ------------------------------------- -------
protected void exportFile(File file) {
logger.debug("file=" + file);
HttpServletResponse response = ServletActionContext.getResponse();
ServletOutputStream out = null;
try {
byte[] fileData = FileUtils.readFileToByteArray(file);
String filename = response.encodeURL(file.getName());
logger.debug("export.file.length=" + fileData.length);
response.reset();
response.setContentType("application/octet-stream");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-transform, max-age=0");
response.setHeader("Accept-Ranges", "none");
response.setHeader("Content-Disposition", "attachment; filename="
+ filename);
out = response.getOutputStream();
out.write(fileData);
out.flush();
} catch (Exception e) {
logger.error("输出文件时出错! " + file.getAbsolutePath(), e);
} finally {
IOUtils.closeQuietly(out);
}
}
------ For reference only ------------------------- --------------
is not refresh the page repeatedly repeated requests .
I have encountered a similar problem that has been refreshed a page address , resulting in the background this error .
------ For reference only -------------------------------------- -
没有评论:
发表评论