下载进度条

@SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping
    public ResponseEntity<String> downFile(
    		
    		HttpServletResponse response,
    		HttpServletRequest request){
    	InputStream inputStream = null;
    	ServletOutputStream out = null;
        try {
        	String filePath = Constants.UPLOAD_FILE.replaceAll("\\\\", "/")+"/mysql-essential-5.0.87-win32.zip";
            File file = new File(filePath);
            int fSize = Integer.parseInt(String.valueOf(file.length()));  
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/x-download");  
            response.setHeader("Accept-Ranges", "bytes");  
            response.setHeader("Content-Length", String.valueOf(fSize));  
            response.setHeader("Content-Disposition", "attachment;fileName=" +"c18462a35a7af69a3eea94f84b7d6a46.zip" );
            inputStream=new FileInputStream(filePath);
            long pos = 0;
            if (null != request.getHeader("Range")) {
                // 断点续传
                response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);  
                try {  
                    pos = Long.parseLong(request.getHeader("Range").replaceAll("bytes=", "").replaceAll("-", ""));  
                } catch (NumberFormatException e) {
                    pos = 0;  
                }  
            }  
            out = response.getOutputStream();  
            String contentRange = new StringBuffer("bytes ").append(pos+"").append("-").append((fSize - 1)+"").append("/").append(fSize+"").toString();
            response.setHeader("Content-Range", contentRange);  
            inputStream.skip(pos);  
            byte[] buffer = new byte[1024*10];
            int length = 0;  
            while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {  
                out.write(buffer, 0, length);
    	   /*<span style="font-family: Consolas;">*/Thread.sleep(100/*</span><code class="java value">100</code><code class="java plain">*/);/*</code>*/
            }
        } catch (Exception e) {
    		//logger.error("ODEX软件下载异常:"+e);
    	}finally{
            try {
            	if(null != out) out.flush();
            	if(null != out) out.close();
            	if(null != inputStream) inputStream.close(); 
    		} catch (IOException e) {
    		}
    	}
        return new ResponseEntity(null,HttpStatus.OK);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值