@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);
}
下载进度条
最新推荐文章于 2024-01-28 15:07:20 发布