修复ueditor中的BUG和视频流的BUG

This commit is contained in:
wenc000 2020-04-05 00:35:00 +08:00
parent 9286c51f8f
commit ca4aaa6119
5 changed files with 55 additions and 10 deletions

View File

@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.Map; import java.util.Map;
@ -131,14 +132,17 @@ public class FileRouteController extends AbstractController {
}) })
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@GetMapping("downloadfile/{isOpen}/{fileId}") @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)) { if (!ISystemConstant.IS_TRUE.equals(isOpen) && !ISystemConstant.IS_FALSE.equals(isOpen)) {
throw new ParamsException("参数错误"); throw new ParamsException("参数错误");
} }
if (fileId.indexOf(".") > 0) {
fileId = fileId.split("\\.")[0];
}
Map<String, Object> params = requestParams(); Map<String, Object> params = requestParams();
params.put("fileId", fileId); params.put("fileId", fileId);
params.put("isOpen", isOpen); params.put("isOpen", isOpen);
fileService.downLoadFile(response, params); fileService.downLoadFile(request, response, params);
} }
/** /**

View File

@ -120,6 +120,16 @@ public interface IFileService {
*/ */
void downLoadFile(HttpServletResponse response, Map<String, Object> params) throws FileException; void downLoadFile(HttpServletResponse response, Map<String, Object> params) throws FileException;
/**
* 文件下载
*
* @param request
* @param response
* @param params
* @throws FileException
*/
void downLoadFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> params) throws FileException;
/** /**
* 百度富文本编辑器 * 百度富文本编辑器
* *
@ -170,6 +180,7 @@ public interface IFileService {
/** /**
* 通过文件ID获取ID列表 * 通过文件ID获取ID列表
*
* @param idList * @param idList
* @return * @return
* @throws SearchException * @throws SearchException

View File

@ -79,6 +79,7 @@ public class FileServiceImpl extends AbstractService implements IFileService {
fileDTO.setFileId(params.get("fileId").toString()); fileDTO.setFileId(params.get("fileId").toString());
fileDTO.setFileName(params.get("fileName").toString()); fileDTO.setFileName(params.get("fileName").toString());
fileDTO.setFileUrl(params.get("fileUrl").toString()); fileDTO.setFileUrl(params.get("fileUrl").toString());
fileDTO.setFileType(params.get("fileType").toString());
return fileDTO; return fileDTO;
} }
@ -174,6 +175,11 @@ public class FileServiceImpl extends AbstractService implements IFileService {
@Override @Override
public void downLoadFile(HttpServletResponse response, Map<String, Object> params) throws FileException { public void downLoadFile(HttpServletResponse response, Map<String, Object> params) throws FileException {
downLoadFile(null, response, params);
}
@Override
public void downLoadFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> params) throws FileException {
OutputStream outputStream = null; OutputStream outputStream = null;
FileInputStream fileInputStream = null; FileInputStream fileInputStream = null;
try { try {
@ -186,15 +192,17 @@ public class FileServiceImpl extends AbstractService implements IFileService {
response.setHeader("Content-Length", filePO.getFileSize()); response.setHeader("Content-Length", filePO.getFileSize());
if (!Boolean.valueOf(params.get("isOpen").toString())) { if (!Boolean.valueOf(params.get("isOpen").toString())) {
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filePO.getFileName(), "UTF-8")); 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(); outputStream = response.getOutputStream();
for (byte[] buf = new byte[INPUT_STREAM_SIZE]; fileInputStream.read(buf) > -1; ) { for (byte[] buf = new byte[INPUT_STREAM_SIZE]; fileInputStream.read(buf) > -1; ) {
outputStream.write(buf, 0, buf.length); outputStream.write(buf, 0, buf.length);
} }
outputStream.flush(); outputStream.flush();
} catch (Exception e) { } catch (Exception e) {
LOG.error(e.getMessage(), e); throw new FileException("文件下载异常", e);
throw new FileException("文件下载异常");
} finally { } finally {
try { try {
if (null != outputStream) { if (null != outputStream) {
@ -204,11 +212,33 @@ public class FileServiceImpl extends AbstractService implements IFileService {
fileInputStream.close(); fileInputStream.close();
} }
} catch (Exception e1) { } 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 @Override
public JSONObject uEditor(MultipartFile file, HttpServletRequest request, Map<String, Object> params) throws SystemException, IOException { public JSONObject uEditor(MultipartFile file, HttpServletRequest request, Map<String, Object> params) throws SystemException, IOException {
if (params.get(UEDITOR_ACTION) == null || StringUtils.isBlank(params.get(UEDITOR_ACTION).toString())) { 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<Map<String, String>> fileList = new ArrayList<>(); List<Map<String, String>> fileList = new ArrayList<>();
for (FileDTO fileDTO : listFileDTOs) { for (FileDTO fileDTO : listFileDTOs) {
Map<String, String> fileMap = new HashMap<>(0); Map<String, String> 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); fileList.add(fileMap);
} }
JSONObject uEditorListResult = new JSONObject(); JSONObject uEditorListResult = new JSONObject();
@ -304,7 +334,7 @@ public class FileServiceImpl extends AbstractService implements IFileService {
fileDTO = uploadSingleForFileDTO(null, file, UploadTypeEnum.IMAGE, params); fileDTO = uploadSingleForFileDTO(null, file, UploadTypeEnum.IMAGE, params);
} else if (UEDITOR_UPLOAD_VIDEO.equals(action)) { } else if (UEDITOR_UPLOAD_VIDEO.equals(action)) {
fileDTO = uploadSingleForFileDTO(null, file, UploadTypeEnum.VIDEO, params); fileDTO = uploadSingleForFileDTO(null, file, UploadTypeEnum.VIDEO, params);
downloadFile = "false"; downloadFile = "true";
} else if (UEDITOR_UPLOAD_FILE.equals(action)) { } else if (UEDITOR_UPLOAD_FILE.equals(action)) {
fileDTO = uploadSingleForFileDTO(null, file, UploadTypeEnum.FILE, params); fileDTO = uploadSingleForFileDTO(null, file, UploadTypeEnum.FILE, params);
downloadFile = "false"; downloadFile = "false";
@ -312,7 +342,7 @@ public class FileServiceImpl extends AbstractService implements IFileService {
if (fileDTO == null) { if (fileDTO == null) {
throw new SystemException("文件上传失败"); 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(); JSONObject fileUploadResult = new JSONObject();
fileUploadResult.put("state", "SUCCESS"); fileUploadResult.put("state", "SUCCESS");
fileUploadResult.put("url", fileUrl); fileUploadResult.put("url", fileUrl);
@ -517,7 +547,7 @@ public class FileServiceImpl extends AbstractService implements IFileService {
contentType = "image/gif"; contentType = "image/gif";
break; break;
case "mp4": case "mp4":
contentType = "video/mpeg4"; contentType = "video/mp4";
break; break;
case "rmvb": case "rmvb":
contentType = "application/vnd.rn-realmedia-vbr"; contentType = "application/vnd.rn-realmedia-vbr";

View File

@ -3,7 +3,7 @@
/* */ /* */
"imageActionName": "uploadimage", /* action */ "imageActionName": "uploadimage", /* action */
"imageFieldName": "file", /* */ "imageFieldName": "file", /* */
"imageMaxSize": 10240000000, /* B */ "imageMaxSize": 102400000, /* B */
"imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* */ "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* */
"imageCompressEnable": true, /* ,true */ "imageCompressEnable": true, /* ,true */
"imageCompressBorder": 1600, /* */ "imageCompressBorder": 1600, /* */