效果图
后台
@Action("billIpsDownload")
public void billIpsDownload() throws IOException, ServiceException {
HttpServletResponse response = this.getResponse();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + test);
response.setHeader("Set-Cookie", "fileDownload=true; path=/");
response.setContentType("application/pdf");
//Add by Nicole 20180413
Cookie status = new Cookie("updateStatus","success");
status.setMaxAge(600);
response.addCookie(status);
ServletOutputStream sos = response.getOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(getContent("D:\\test\\Watermark.pdf"));
IOUtils.copy(in, sos);
in.close();
sos.close();
}
public byte[] getContent(String filePath) throws IOException {
File file = new File(filePath);
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
System.out.println("file too big...");
return null;
}
FileInputStream fi = new FileInputStream(file);
byte[] buffer = new byte[(int) fileSize];
int offset = 0;
int numRead = 0;
while (offset < buffer.length && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
// 确保所有数据均被读取
if (offset != buffer.length) {
throw new IOException("Could not completely read file " + file.getName());
}
fi.close();
return buffer;
}
前台页面
<script type="text/javascript">
var timer1 = setInterval(refeshPage,1000);
function refeshPage(){
if(getCookie("updateStatus")=="success"){
clearInterval(timer1);
delCookie("updateStatus");
location.reload(true);
}
}
</script>