获取文件封面
接口信息
以字节流的方式写回前端
定义Controller
在 FileInfoController 下定义查询方法,获取文件封面。
@GetMapping("/getImage/{imageFolder}/{imageName}") public void getImage(HttpServletResponse response, @PathVariable("imageFolder") @NotNull String imageFolder, @PathVariable("imageName") @NotNull String imageName) { String imagePath = appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE + imageFolder + "/" + imageName; FileUtils.writeImage(response, imagePath); }
|
获取视频文件
接口信息
以字节流的方式写回前端
定义Service
找到 FileInfoService,定义获取文件路径方法
FileInfoService
String getFilePath(String id, String userId);
|
在 FileInfoServiceImpl 中实现该方法,如果获取的是.ts切片文件,则根据.ts文件名获取该文件ID,从数据库查询后,返回其所在目录;如果是视频文件,则返回其.m3u8文件;其他文件直接返回。
FileInfoServiceImpl
@Override public String getFilePath(String id, String userId) { String filePath = null; if (id.endsWith(".ts")) { String[] tsArray = id.split("_"); String realFileId = tsArray[0]; FileInfo fileInfo = this.getOne(new LambdaQueryWrapper<FileInfo>().eq(FileInfo::getId, realFileId).eq(FileInfo::getUserId, userId)); String fileName = appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE + fileInfo.getFilePath(); String folderPath = StringTools.getFilename(fileName); filePath = folderPath + "/" + id; } else { FileInfo fileInfo = this.getOne(new LambdaQueryWrapper<FileInfo>().eq(FileInfo::getId, id).eq(FileInfo::getUserId, userId)); if (null == fileInfo) { throw new BizException("文件不存在"); } if (FileCategoryEnums.VIDEO.getCategory().equals(fileInfo.getFileCategory())) { String fileName = appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE + fileInfo.getFilePath(); String folderPath = StringTools.getFilename(fileName); filePath = folderPath + "/" + Constants.M3U8_NAME; } else { filePath = appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE + fileInfo.getFilePath(); } } return filePath; }
|
定义Controller
在 FileInfoController 下定义查询方法,获取文件封面。
@GetMapping("/getImage/{imageFolder}/{imageName}") public void getImage(HttpServletResponse response, @PathVariable("imageFolder") @NotNull String imageFolder, @PathVariable("imageName") @NotNull String imageName) { String imagePath = appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE + imageFolder + "/" + imageName; FileUtils.writeImage(response, imagePath); }
|
获取文件
接口信息
以字节流的方式写回前端
定义Controller
在 FileInfoController 下定义查询方法,获取文件流。
@GetMapping("/getFile/{id}") public void getFile(HttpSession session, HttpServletResponse response, @PathVariable("id") @NotBlank String id) { SessionWebUserVO user = (SessionWebUserVO) session.getAttribute(Constants.SESSION_KEY); String filePath = fileInfoService.getFilePath(id, user.getId()); FileUtil.readFile(response, filePath); }
|