调整了文件下载
This commit is contained in:
parent
9a3fb3e060
commit
7361a0d0d3
@ -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
|
||||
|
@ -185,40 +185,49 @@ public class FileServiceImpl extends AbstractService implements IFileService {
|
||||
|
||||
@Override
|
||||
public void downLoadFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> 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
|
||||
|
@ -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());
|
||||
|
Loading…
Reference in New Issue
Block a user