java文件断点续传下载或视频播放
程序员文章站
2022-10-03 14:26:35
java文件断点续传下载或视频播放代码代码@GetMapping("/download")public void download(Long id, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {Tfile findTfile = fileService.getById(id);//获取路径String fullPath = findTfile.g...
java文件断点续传下载或视频播放
代码
@GetMapping("/download") public void download(Long id, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException { Tfile findTfile = fileService.getById(id); //获取路径 String fullPath = findTfile.getPath(); //创建本地文件对象 File downloadFile = new File(fullPath); // 请求数据范围字符串 String range = request.getHeader("Range"); long startByte = 0; long endByte = downloadFile.length() - 1; if (range != null && range.contains("bytes=") && range.contains("-")) { String rangeFormat = range.substring(range.lastIndexOf("=") + 1).trim(); String ranges[] = rangeFormat.split("-"); try { if (rangeFormat.startsWith("-")) { endByte = Long.parseLong(ranges[1]); } else if (rangeFormat.endsWith("-")) { startByte = Long.parseLong(ranges[0]); } else if (ranges.length == 2) { startByte = Long.parseLong(ranges[0]); endByte = Long.parseLong(ranges[1]); } } catch (NumberFormatException e) { e.printStackTrace(); startByte = 0; endByte = downloadFile.length() - 1; } } long contentLength = endByte - startByte + 1; response.setHeader("Accept-Ranges", "bytes"); response.setContentType("application/octet-stream"); //文件名 String filename = findTfile.getName(); try { if (request.getHeader("User-Agent").contains("MSIE")) { filename=URLEncoder.encode(filename,"UTF-8"); }else { filename=new String(filename.getBytes("UTF-8"),"ISO-8859-1"); } } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } response.setHeader("Content-Disposition", String.format("attachment;filename=\"%s\"", filename)); response.setHeader("Content-Length", String.valueOf(contentLength)); if (range != null && range.contains("bytes=") && range.contains("-")) { response.setStatus(response.SC_PARTIAL_CONTENT); response.setHeader("Content-Range", "bytes " + startByte + "-" + endByte + "/" + downloadFile.length()); } BufferedOutputStream outputStream = null; RandomAccessFile randomAccessFile = null; long transmitted = 0; try { randomAccessFile = new RandomAccessFile(downloadFile, "r"); outputStream = new BufferedOutputStream(response.getOutputStream()); int bufLen = (int) (contentLength < 2048 ? contentLength : 2048); byte[] buff = new byte[4096]; int len = 0; randomAccessFile.seek(startByte); while ((transmitted + len) <= contentLength && (len = randomAccessFile.read(buff)) != -1) { outputStream.write(buff, 0, len); transmitted += len; } if (transmitted < contentLength) { len = randomAccessFile.read(buff, 0, (int) (contentLength - transmitted)); outputStream.write(buff, 0, len); transmitted += len; } outputStream.flush(); response.flushBuffer(); randomAccessFile.close(); } catch (ClientAbortException e) { } catch (IOException e) { e.printStackTrace(); } finally { try { if (randomAccessFile != null) { randomAccessFile.close(); } } catch (IOException e) { e.printStackTrace(); } } }
本文地址:https://blog.csdn.net/weixin_45676137/article/details/108239530
下一篇: 钩弋夫人的一生是幸还是不幸?该怎么评价