From e03d2b41f0fd405acb409d6dc753c805d0f967aa Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Thu, 11 Jun 2020 17:45:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=96=AD=E7=82=B9=E7=BB=AD?= =?UTF-8?q?=E4=BC=A0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/file/impl/FileServiceImpl.java | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) 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 5487bd4..6a1d899 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 @@ -25,6 +25,7 @@ import it.sauronsoftware.jave.Encoder; import it.sauronsoftware.jave.EncoderException; import it.sauronsoftware.jave.MultimediaInfo; import net.coobird.thumbnailator.Thumbnails; +import org.apache.catalina.connector.ClientAbortException; import org.apache.commons.fileupload.disk.DiskFileItem; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -190,7 +191,7 @@ public class FileServiceImpl extends AbstractService implements IFileService { throw new SearchException("文件获取失败"); } try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(filePO.getFilePath()))); - OutputStream outputStream = response.getOutputStream(); + OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); ) { boolean isOpen = Boolean.valueOf(params.get("isOpen").toString()); response.setHeader("Content-Length", filePO.getFileSize()); @@ -200,8 +201,11 @@ public class FileServiceImpl extends AbstractService implements IFileService { } else { response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(filePO.getFileName(), "UTF-8")); } - String rangeString = request.getHeader("Range"); - LOG.debug("range: {}", rangeString); + String rangeString = null; + if (request != null) { + rangeString = request.getHeader("Range"); + LOG.debug("range: {}", rangeString); + } long contentLength = Long.valueOf(filePO.getFileSize()); long startRange = 0; long endRange = contentLength - 1; @@ -212,7 +216,7 @@ public class FileServiceImpl extends AbstractService implements IFileService { String[] rangeArray = rangeString.substring(rangeString.indexOf("=") + 1).split("-"); startRange = Long.valueOf(rangeArray[0]); if (rangeArray.length > 1) { - endRange = Long.valueOf(rangeArray[1]) - 1; + endRange = Long.valueOf(rangeArray[1]); } setRangeHeader(startRange, endRange, response, filePO.getFileId(), contentLength); bufferedInputStream.skip(startRange); @@ -220,21 +224,28 @@ public class FileServiceImpl extends AbstractService implements IFileService { 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; + int readLength; + for (byte[] buf = new byte[INPUT_STREAM_SIZE]; (readLength = bufferedInputStream.read(buf)) > -1 && outputLength < totalOutputLength; ) { + long remainingLength = totalOutputLength - outputLength; + int bufSize = remainingLength < INPUT_STREAM_SIZE ? (int) remainingLength : readLength; + outputStream.write(buf, 0, bufSize); + outputLength += bufSize; } outputStream.flush(); } catch (Exception e) { - throw new FileException("文件输出异常"); + if (e instanceof ClientAbortException) { + LOG.debug("客户端断开连接"); + } else { + throw new FileException("文件输出异常", e); + } } } /** * 处理视频流问题 * - * @param request + * @param startRange + * @param endRange * @param response * @param fileId * @param contentLength