经典笑话,爆笑笑话,男女笑话,冷笑话-幽默笑话集

幽默笑话网 > 人生哲理 >

java/servlet 断点下载

//被下载文件路径名称 String path = "D:\\TDDownload\\system\\",fileName="cn_windows_8_1_pro_vl_x86_dvd_2972620.iso"; File file = new File(path+fileName); InputStream in = new FileInputStream(file); OutputStream os= resp.getOutputStream(); //清空response 缓存中数据,仅保留状态码及Header resp.reset(); byte [] buffer = new byte [128]; long downloaded=0; String range = req.getHeader("range"); if(range != null){ downloaded = Long.parseLong(range.split("-")[0].split("=")[1]); //设置 206状态表示可以断点下载 resp.setStatus(206); //设置下载的范围 resp.setHeader("Content-Range","bytes "+downloaded+"-"+file.length()+"/"+file.length()); } resp.setContentType("application/octet-stream"); //文件名 resp.addHeader("Content-Disposition", "attachment; filename="+new String(fileName.getBytes())); //文件大小 resp.addHeader("Content-Length", file.length() + ""); //接受范围 bytes resp.setHeader("Accept-Ranges", "bytes"); int len = 0; while ((len=in.read(buffer))!=-1){ os.write(buffer,0,len); } in.close(); os.flush(); os.close();

(责任编辑:幽默笑话网)