Java playback video if you need background processing, and not request XXX.mp4. When you need to drag the progress bar, some browsers don’t work, so you need to break the transfer.

Directly on the code, via the VIDEO’s SRC request

/** * video request location ** @param request * @param response */ @requestMapping (value = "/player", method = RequestMethod.GET) public void player(HttpServletRequest request, HttpServletResponse response) { String path = request.getServletContext().getRealPath("/static/my/video/test.mp4"); BufferedInputStream bis = null; try { File file = new File(path); if (file.exists()) { long p = 0L; long toLength = 0L; long contentLength = 0L; int rangeSwitch = 0; // 0, download the full text from the beginning; 1, download from a byte (bytes=27000-); (bytes=27000-39000) long fileLength; String rangBytes = ""; fileLength = file.length(); // get file content InputStream ins = new FileInputStream(file); bis = new BufferedInputStream(ins); // tell the client to allow accept-ranges response.reset(); response.setHeader("Accept-Ranges", "bytes"); // client requests a file block download start byte String range = request.getHeader("Range"); if (range ! = null && range.trim().length() > 0 && !" null".equals(range)) { response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT); rangBytes = range.replaceAll("bytes=", ""); if (rangBytes.endsWith("-")) { // bytes=270000- rangeSwitch = 1; p = Long.parseLong(rangBytes.substring(0, rangBytes.indexOf("-"))); contentLength = fileLength - p; } else {// Bytes =270000-320000 rangeSwitch =2; // Bytes =270000-320000 rangeSwitch =2; String temp1 = rangBytes.substring(0, rangBytes.indexOf("-")); String temp2 = rangBytes.substring(rangBytes.indexOf("-") + 1, rangBytes.length()); p = Long.parseLong(temp1); toLength = Long.parseLong(temp2); contentLength = toLength - p + 1; // The client requests bytes between 270000-320000}} else {contentLength = fileLength; } // If content-length is set, the client will automatically do multithreaded downloads. Do not set this parameter if you do not want to support multithreading. // Content-Length: Response.setheader (" content-Length ", new Long(contentLength).toString()); // The response format is: // content-range: Bytes [start bytes of file block]-[total size of file - 1]/[Total size of file] if (rangeSwitch == 1) {String contentRange = new StringBuffer("bytes ").append(new Long(p).toString()).append("-") .append(new Long(fileLength - 1).toString()).append("/") .append(new Long(fileLength).toString()).toString(); response.setHeader("Content-Range", contentRange); bis.skip(p); } else if (rangeSwitch == 2) { String contentRange = range.replace("=", " ") + "/" + new Long(fileLength).toString(); response.setHeader("Content-Range", contentRange); bis.skip(p); } else { String contentRange = new StringBuffer("bytes ").append("0-").append(fileLength - 1).append("/") .append(fileLength).toString(); response.setHeader("Content-Range", contentRange); } String fileName = file.getName(); response.setContentType("application/octet-stream"); response.addHeader("Content-Disposition", "attachment; filename=" + fileName); OutputStream out = response.getOutputStream(); int n = 0; long readLength = 0; int bsize = 1024; byte[] bytes = new byte[bsize]; If (rangeSwitch == 2) {// For bytes=27000-39000, While (readLength <= contentLength - bsize) {n = bis.read(bytes); readLength += n; out.write(bytes, 0, n); } if (readLength <= contentLength) { n = bis.read(bytes, 0, (int) (contentLength - readLength)); out.write(bytes, 0, n); } } else { while ((n = bis.read(bytes)) ! = -1) { out.write(bytes, 0, n); } } out.flush(); out.close(); bis.close(); }} catch (IOException ie) {// Ignore ClientAbortException like} catch (Exception e) {e.printStackTrace(); }}Copy the code