diff --git a/cloud-common-plugin-dynamic/src/main/resources/assets.zip b/cloud-common-plugin-dynamic/src/main/resources/assets.zip index 1135ba4..06017f4 100644 Binary files a/cloud-common-plugin-dynamic/src/main/resources/assets.zip and b/cloud-common-plugin-dynamic/src/main/resources/assets.zip differ diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/routes/file/FileRouteController.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/routes/file/FileRouteController.java index 7ccd726..af347d5 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/routes/file/FileRouteController.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/routes/file/FileRouteController.java @@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Map; @@ -131,14 +132,17 @@ public class FileRouteController extends AbstractController { }) @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @GetMapping("downloadfile/{isOpen}/{fileId}") - public void downLoadFile(HttpServletResponse response, @PathVariable("isOpen") String isOpen, @PathVariable("fileId") String fileId) throws FileException, ParamsException { + public void downLoadFile(HttpServletRequest request, HttpServletResponse response, @PathVariable("isOpen") String isOpen, @PathVariable("fileId") String fileId) throws FileException, ParamsException { if (!ISystemConstant.IS_TRUE.equals(isOpen) && !ISystemConstant.IS_FALSE.equals(isOpen)) { throw new ParamsException("参数错误"); } + if (fileId.indexOf(".") > 0) { + fileId = fileId.split("\\.")[0]; + } Map params = requestParams(); params.put("fileId", fileId); params.put("isOpen", isOpen); - fileService.downLoadFile(response, params); + fileService.downLoadFile(request, response, params); } /** diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java index 2e0597c..c90283d 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java @@ -120,6 +120,16 @@ public interface IFileService { */ void downLoadFile(HttpServletResponse response, Map params) throws FileException; + /** + * 文件下载 + * + * @param request + * @param response + * @param params + * @throws FileException + */ + void downLoadFile(HttpServletRequest request, HttpServletResponse response, Map params) throws FileException; + /** * 百度富文本编辑器 * @@ -170,6 +180,7 @@ public interface IFileService { /** * 通过文件ID获取ID列表 + * * @param idList * @return * @throws SearchException diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java index d5845ff..bbb9d3c 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java @@ -79,6 +79,7 @@ public class FileServiceImpl extends AbstractService implements IFileService { fileDTO.setFileId(params.get("fileId").toString()); fileDTO.setFileName(params.get("fileName").toString()); fileDTO.setFileUrl(params.get("fileUrl").toString()); + fileDTO.setFileType(params.get("fileType").toString()); return fileDTO; } @@ -174,6 +175,11 @@ public class FileServiceImpl extends AbstractService implements IFileService { @Override public void downLoadFile(HttpServletResponse response, Map params) throws FileException { + downLoadFile(null, response, params); + } + + @Override + public void downLoadFile(HttpServletRequest request, HttpServletResponse response, Map params) throws FileException { OutputStream outputStream = null; FileInputStream fileInputStream = null; try { @@ -186,15 +192,17 @@ public class FileServiceImpl extends AbstractService implements IFileService { response.setHeader("Content-Length", filePO.getFileSize()); if (!Boolean.valueOf(params.get("isOpen").toString())) { response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filePO.getFileName(), "UTF-8")); + } else { + response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(filePO.getFileName(), "UTF-8")); } + setRangeHeader(request, response, filePO.getFileId(), Long.valueOf(filePO.getFileSize())); outputStream = response.getOutputStream(); for (byte[] buf = new byte[INPUT_STREAM_SIZE]; fileInputStream.read(buf) > -1; ) { outputStream.write(buf, 0, buf.length); } outputStream.flush(); } catch (Exception e) { - LOG.error(e.getMessage(), e); - throw new FileException("文件下载异常"); + throw new FileException("文件下载异常", e); } finally { try { if (null != outputStream) { @@ -204,11 +212,33 @@ public class FileServiceImpl extends AbstractService implements IFileService { fileInputStream.close(); } } catch (Exception e1) { - LOG.error(e1.getMessage(), e1); + throw new FileException("流关闭异常", e1); } } } + /** + * 处理视频流问题 + * + * @param request + * @param response + * @param fileId + * @param contentLength + */ + private void setRangeHeader(HttpServletRequest request, HttpServletResponse response, String fileId, long contentLength) { + if (request == null) { + return; + } + String rangeString = request.getHeader("Range"); + if (StringUtils.isBlank(rangeString)) { + return; + } + long range = Long.valueOf(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-"))); + response.setHeader("Content-Range", String.valueOf(range + (contentLength - 1))); + response.setHeader("Accept-Ranges", "bytes"); + response.setHeader("Etag", fileId); + } + @Override public JSONObject uEditor(MultipartFile file, HttpServletRequest request, Map params) throws SystemException, IOException { if (params.get(UEDITOR_ACTION) == null || StringUtils.isBlank(params.get(UEDITOR_ACTION).toString())) { @@ -275,7 +305,7 @@ public class FileServiceImpl extends AbstractService implements IFileService { List> fileList = new ArrayList<>(); for (FileDTO fileDTO : listFileDTOs) { Map fileMap = new HashMap<>(0); - fileMap.put("url", String.format("%s/route/file/downloadfile/%s/%s", request.getContextPath(), downloadFile, fileDTO.getFileId())); + fileMap.put("url", String.format("%s/route/file/downloadfile/%s/%s.%s", request.getContextPath(), downloadFile, fileDTO.getFileId(), fileDTO.getFileType())); fileList.add(fileMap); } JSONObject uEditorListResult = new JSONObject(); @@ -304,7 +334,7 @@ public class FileServiceImpl extends AbstractService implements IFileService { fileDTO = uploadSingleForFileDTO(null, file, UploadTypeEnum.IMAGE, params); } else if (UEDITOR_UPLOAD_VIDEO.equals(action)) { fileDTO = uploadSingleForFileDTO(null, file, UploadTypeEnum.VIDEO, params); - downloadFile = "false"; + downloadFile = "true"; } else if (UEDITOR_UPLOAD_FILE.equals(action)) { fileDTO = uploadSingleForFileDTO(null, file, UploadTypeEnum.FILE, params); downloadFile = "false"; @@ -312,7 +342,7 @@ public class FileServiceImpl extends AbstractService implements IFileService { if (fileDTO == null) { throw new SystemException("文件上传失败"); } - String fileUrl = String.format("route/file/downloadfile/%s/%s", downloadFile, fileDTO.getFileId()); + String fileUrl = String.format("route/file/downloadfile/%s/%s.%s", downloadFile, fileDTO.getFileId(), fileDTO.getFileType()); JSONObject fileUploadResult = new JSONObject(); fileUploadResult.put("state", "SUCCESS"); fileUploadResult.put("url", fileUrl); @@ -517,7 +547,7 @@ public class FileServiceImpl extends AbstractService implements IFileService { contentType = "image/gif"; break; case "mp4": - contentType = "video/mpeg4"; + contentType = "video/mp4"; break; case "rmvb": contentType = "application/vnd.rn-realmedia-vbr"; diff --git a/cloud-common-plugin/src/main/resources/ueditorconfig.json b/cloud-common-plugin/src/main/resources/ueditorconfig.json index 230cf64..9aeeabd 100644 --- a/cloud-common-plugin/src/main/resources/ueditorconfig.json +++ b/cloud-common-plugin/src/main/resources/ueditorconfig.json @@ -3,7 +3,7 @@ /* 上传图片配置项 */ "imageActionName": "uploadimage", /* 执行上传图片的action名称 */ "imageFieldName": "file", /* 提交的图片表单名称 */ - "imageMaxSize": 10240000000, /* 上传大小限制,单位B */ + "imageMaxSize": 102400000, /* 上传大小限制,单位B */ "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */ "imageCompressEnable": true, /* 是否压缩图片,默认是true */ "imageCompressBorder": 1600, /* 图片压缩最长边限制 */