Java实现文件下载后,利用cookie定时即时刷新页面table的某些字段

效果图


后台

@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>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值