解决断点续传问题
This commit is contained in:
parent
7361a0d0d3
commit
e03d2b41f0
@ -25,6 +25,7 @@ import it.sauronsoftware.jave.Encoder;
|
|||||||
import it.sauronsoftware.jave.EncoderException;
|
import it.sauronsoftware.jave.EncoderException;
|
||||||
import it.sauronsoftware.jave.MultimediaInfo;
|
import it.sauronsoftware.jave.MultimediaInfo;
|
||||||
import net.coobird.thumbnailator.Thumbnails;
|
import net.coobird.thumbnailator.Thumbnails;
|
||||||
|
import org.apache.catalina.connector.ClientAbortException;
|
||||||
import org.apache.commons.fileupload.disk.DiskFileItem;
|
import org.apache.commons.fileupload.disk.DiskFileItem;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -190,7 +191,7 @@ public class FileServiceImpl extends AbstractService implements IFileService {
|
|||||||
throw new SearchException("文件获取失败");
|
throw new SearchException("文件获取失败");
|
||||||
}
|
}
|
||||||
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(filePO.getFilePath())));
|
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());
|
boolean isOpen = Boolean.valueOf(params.get("isOpen").toString());
|
||||||
response.setHeader("Content-Length", filePO.getFileSize());
|
response.setHeader("Content-Length", filePO.getFileSize());
|
||||||
@ -200,8 +201,11 @@ public class FileServiceImpl extends AbstractService implements IFileService {
|
|||||||
} else {
|
} else {
|
||||||
response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(filePO.getFileName(), "UTF-8"));
|
response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(filePO.getFileName(), "UTF-8"));
|
||||||
}
|
}
|
||||||
String rangeString = request.getHeader("Range");
|
String rangeString = null;
|
||||||
LOG.debug("range: {}", rangeString);
|
if (request != null) {
|
||||||
|
rangeString = request.getHeader("Range");
|
||||||
|
LOG.debug("range: {}", rangeString);
|
||||||
|
}
|
||||||
long contentLength = Long.valueOf(filePO.getFileSize());
|
long contentLength = Long.valueOf(filePO.getFileSize());
|
||||||
long startRange = 0;
|
long startRange = 0;
|
||||||
long endRange = contentLength - 1;
|
long endRange = contentLength - 1;
|
||||||
@ -212,7 +216,7 @@ public class FileServiceImpl extends AbstractService implements IFileService {
|
|||||||
String[] rangeArray = rangeString.substring(rangeString.indexOf("=") + 1).split("-");
|
String[] rangeArray = rangeString.substring(rangeString.indexOf("=") + 1).split("-");
|
||||||
startRange = Long.valueOf(rangeArray[0]);
|
startRange = Long.valueOf(rangeArray[0]);
|
||||||
if (rangeArray.length > 1) {
|
if (rangeArray.length > 1) {
|
||||||
endRange = Long.valueOf(rangeArray[1]) - 1;
|
endRange = Long.valueOf(rangeArray[1]);
|
||||||
}
|
}
|
||||||
setRangeHeader(startRange, endRange, response, filePO.getFileId(), contentLength);
|
setRangeHeader(startRange, endRange, response, filePO.getFileId(), contentLength);
|
||||||
bufferedInputStream.skip(startRange);
|
bufferedInputStream.skip(startRange);
|
||||||
@ -220,21 +224,28 @@ public class FileServiceImpl extends AbstractService implements IFileService {
|
|||||||
LOG.debug("startRange: {}, endRange: {}", startRange, endRange);
|
LOG.debug("startRange: {}, endRange: {}", startRange, endRange);
|
||||||
long totalOutputLength = endRange - startRange + 1;
|
long totalOutputLength = endRange - startRange + 1;
|
||||||
long outputLength = 0;
|
long outputLength = 0;
|
||||||
int readLength = 0;
|
int readLength;
|
||||||
for (byte[] buf = new byte[INPUT_STREAM_SIZE]; (readLength = bufferedInputStream.read(buf)) > -1 && outputLength <= totalOutputLength; ) {
|
for (byte[] buf = new byte[INPUT_STREAM_SIZE]; (readLength = bufferedInputStream.read(buf)) > -1 && outputLength < totalOutputLength; ) {
|
||||||
outputStream.write(buf, 0, buf.length);
|
long remainingLength = totalOutputLength - outputLength;
|
||||||
outputLength += readLength;
|
int bufSize = remainingLength < INPUT_STREAM_SIZE ? (int) remainingLength : readLength;
|
||||||
|
outputStream.write(buf, 0, bufSize);
|
||||||
|
outputLength += bufSize;
|
||||||
}
|
}
|
||||||
outputStream.flush();
|
outputStream.flush();
|
||||||
} catch (Exception e) {
|
} 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 response
|
||||||
* @param fileId
|
* @param fileId
|
||||||
* @param contentLength
|
* @param contentLength
|
||||||
|
Loading…
Reference in New Issue
Block a user