1.页面浏览器请求:
2. 读取文件:
java">@GetMapping("/read")
@ApiOperation(value = "视频读取", httpMethod = "GET", response = ResponseMessage.class, notes = "tst")
public void test(HttpServletRequest request, HttpServletResponse response, @RequestParam String filePath) {if (filePath.contains("%")) {try {filePath = URLDecoder.decode(filePath, "UTF-8");} catch (UnsupportedEncodingException e) {}}if (filePath.contains("%")) {try {filePath = URLDecoder.decode(filePath, "UTF-8");} catch (UnsupportedEncodingException e) {}}ServletOutputStream out = null;FileInputStream in = null;try {String path = systemProperties.getDir() + FilenameUtils.getName(filePath);readFileAsResponse(request, response, new File(path), "");} catch (FileNotFoundException e) {} catch (Exception e) {} finally {try {out.close();in.close();} catch (NullPointerException e) {} catch (Exception e) {}}
}
java"> /*** 播放把文件作为流返回给客户端** @throws IOException*/
public static void readFileAsResponse(HttpServletRequest request, HttpServletResponse response, File readfile, String fileName2) throws IOException {InputStream is1 = null;OutputStream os2 = null;try {is1 = new FileInputStream(readfile);long fileSizeNumber = readfile.length();if (StringUtils.isBlank(fileName2)) {fileName2 = readfile.getName();}String suffix = fileName2.substring(fileName2.lastIndexOf(".") + 1);String videoSuffix = "mp4";if (videoSuffix.equalsIgnoreCase(suffix)) {response.setContentType("video/mp4");} else {response.setContentType("application/octet-stream");}response.setHeader("Accept-Ranges", "bytes");String range = request.getHeader("Range");// 返回的状态码,默认200,首次下载int status = HttpServletResponse.SC_OK;// 如果range下载区域为空,则首次下载,if (range == null) {range = "bytes=0-";} else {// 客户发送了一个带有Range头的GET请求status = HttpServletResponse.SC_PARTIAL_CONTENT;}long start = 0, end = 0;String startsWithFlag = "bytes=";if (null != range && range.startsWith(startsWithFlag)) {//处理start为负数和start end为非正整数String rangeStart = range.split("=")[1].substring(0, 1);String equalsFlag = "-";if (equalsFlag.equals(rangeStart)) {ResponseMessage responseMessage = new ResponseMessage();responseMessage.setCode(CodeMessageUtil.CodeEnum.CODE_1039.getCode().toString());responseMessage.setMessage(CodeMessageUtil.CodeEnum.CODE_1039.getDesc());responseMessage.result("失败!");ResponseUtil.setResponse(response, responseMessage.toString());return;}String[] values = range.split("=")[1].split("-");//验证start的整数性 (正则匹配)if (validate(String.valueOf(values[0]), Validation.INT) == false) {ResponseMessage responseMessage = new ResponseMessage();responseMessage.setCode(CodeMessageUtil.CodeEnum.CODE_1039.getCode().toString());responseMessage.setMessage(CodeMessageUtil.CodeEnum.CODE_1039.getDesc());responseMessage.result("失败!");ResponseUtil.setResponse(response, responseMessage.toString());return;}start = Long.parseLong(values[0]);// 如果服务器端没有设置end结尾,默认取下载全部if (values.length == 1) {end = fileSizeNumber - 1;} else {//验证end的整数性(正则匹配)if (validate(String.valueOf(values[1]), Validation.INT) == false) {ResponseMessage responseMessage = new ResponseMessage();responseMessage.setCode(CodeMessageUtil.CodeEnum.CODE_1039.getCode().toString());responseMessage.setMessage(CodeMessageUtil.CodeEnum.CODE_1039.getDesc());responseMessage.result("失败!");ResponseUtil.setResponse(response, responseMessage.toString());return;}end = Long.parseLong(values[1]);//当end设置过长是,把end设置成文件大小if (end + 1 > fileSizeNumber) {end = fileSizeNumber - 1;}}}// 此次数据响应大小long responseSize = 0;if (end != 0 && end > start) {responseSize = end - start + 1;// 返回当前连接下载的数据大小,也就是此次数据传输大小response.addHeader("Content-Length", "" + (responseSize));} else {responseSize = Integer.MAX_VALUE;}byte[] buffer = new byte[4096];// 设置响应状态码response.setStatus(status);// 设置断点续传的Content-Range传输字节和总字节response.addHeader("Content-Range", "bytes " + start + "-" + (fileSizeNumber - 1) + "/" + fileSizeNumber);// 当前需要下载文件的大小long needSize = responseSize;if (start != 0) {// 跳已经传输过的字节is1.skip(start);}os2 = response.getOutputStream();while (needSize > 0) {int len = is1.read(buffer);if (needSize < buffer.length) {os2.write(buffer, 0, (int) needSize);} else {os2.write(buffer, 0, len);// 如果读取文件大小小于缓冲字节大小,表示已写入完,直接跳出if (len < buffer.length) {break;}}// 不断更新当前可下载文件大小needSize -= buffer.length;}os2.flush();} catch (IOException e) {} finally {if (is1 != null) {is1.close();}if (os2 != null) {os2.close();}}
}