From 7361a0d0d3be75388a249891015d9e4dfa62ba26 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Tue, 9 Jun 2020 23:37:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BA=86=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/service/file/IFileService.java | 4 +- .../service/file/impl/FileServiceImpl.java | 72 ++++++++++--------- .../com/cm/common/advice/ResponseAdvice.java | 6 +- 3 files changed, 44 insertions(+), 38 deletions(-) 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 01df96a..e2bd097 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 @@ -42,9 +42,9 @@ public interface IFileService { */ String UPLOAD_AUDIO_TYPE = "4"; /** - * 读取流大小 + * 读取流大小 1M */ - int INPUT_STREAM_SIZE = 1024; + int INPUT_STREAM_SIZE = 1048576; /** * uEditor action 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 032d3a7..5487bd4 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 @@ -185,40 +185,49 @@ public class FileServiceImpl extends AbstractService implements IFileService { @Override public void downLoadFile(HttpServletRequest request, HttpServletResponse response, Map params) throws FileException { - OutputStream outputStream = null; - FileInputStream fileInputStream = null; - try { - FilePO filePO = fileDao.getFile(params); - if (null == filePO) { - throw new SearchException("文件获取失败"); - } - fileInputStream = new FileInputStream(new File(filePO.getFilePath())); - response.setContentType(getContentType(filePO.getFileType())); + FilePO filePO = fileDao.getFile(params); + if (null == filePO) { + throw new SearchException("文件获取失败"); + } + try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(filePO.getFilePath()))); + OutputStream outputStream = response.getOutputStream(); + ) { + boolean isOpen = Boolean.valueOf(params.get("isOpen").toString()); response.setHeader("Content-Length", filePO.getFileSize()); - if (!Boolean.valueOf(params.get("isOpen").toString())) { + response.setContentType(getContentType(filePO.getFileType())); + if (!isOpen) { 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; ) { + String rangeString = request.getHeader("Range"); + LOG.debug("range: {}", rangeString); + long contentLength = Long.valueOf(filePO.getFileSize()); + long startRange = 0; + long endRange = contentLength - 1; + if (!StringUtils.isBlank(rangeString)) { + if (!isOpen) { + response.setContentType("multipart/byteranges"); + } + String[] rangeArray = rangeString.substring(rangeString.indexOf("=") + 1).split("-"); + startRange = Long.valueOf(rangeArray[0]); + if (rangeArray.length > 1) { + endRange = Long.valueOf(rangeArray[1]) - 1; + } + setRangeHeader(startRange, endRange, response, filePO.getFileId(), contentLength); + bufferedInputStream.skip(startRange); + } + LOG.debug("startRange: {}, endRange: {}", startRange, endRange); + long totalOutputLength = endRange - startRange + 1; + long outputLength = 0; + int readLength = 0; + for (byte[] buf = new byte[INPUT_STREAM_SIZE]; (readLength = bufferedInputStream.read(buf)) > -1 && outputLength <= totalOutputLength; ) { outputStream.write(buf, 0, buf.length); + outputLength += readLength; } outputStream.flush(); } catch (Exception e) { - throw new FileException("文件下载异常", e); - } finally { - try { - if (null != outputStream) { - outputStream.close(); - } - if (null != fileInputStream) { - fileInputStream.close(); - } - } catch (Exception e1) { - throw new FileException("流关闭异常", e1); - } + throw new FileException("文件输出异常"); } } @@ -230,18 +239,11 @@ public class FileServiceImpl extends AbstractService implements IFileService { * @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))); + private void setRangeHeader(long startRange, long endRange, HttpServletResponse response, String fileId, long contentLength) { + response.setHeader("Content-Range", String.format("bytes %d-%d/%d", startRange, endRange, contentLength)); response.setHeader("Accept-Ranges", "bytes"); response.setHeader("Etag", fileId); + response.setStatus(206); } @Override diff --git a/cloud-common/src/main/java/com/cm/common/advice/ResponseAdvice.java b/cloud-common/src/main/java/com/cm/common/advice/ResponseAdvice.java index b53b59b..54180af 100644 --- a/cloud-common/src/main/java/com/cm/common/advice/ResponseAdvice.java +++ b/cloud-common/src/main/java/com/cm/common/advice/ResponseAdvice.java @@ -37,10 +37,14 @@ public class ResponseAdvice { @ResponseBody @ExceptionHandler({SystemException.class, SQLSyntaxErrorException.class, Exception.class}) public void responseException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException { - LOG.error(e.getMessage(), e); if (e instanceof InsufficientAuthenticationException) { throw new InsufficientAuthenticationException(e.getMessage()); } + if (e instanceof FileException) { + LOG.error(e.getMessage()); + } else { + LOG.error(e.getMessage(), e); + } ErrorResult result = new ErrorResult(ErrorResultCodeEnum.SYSTEM_ERROR.getValue(), "系统错误"); if (e instanceof SaveException) { result.setCode(ErrorResultCodeEnum.SAVE_ERROR.getValue());