调整了文件下载
This commit is contained in:
parent
9a3fb3e060
commit
7361a0d0d3
@ -42,9 +42,9 @@ public interface IFileService {
|
|||||||
*/
|
*/
|
||||||
String UPLOAD_AUDIO_TYPE = "4";
|
String UPLOAD_AUDIO_TYPE = "4";
|
||||||
/**
|
/**
|
||||||
* 读取流大小
|
* 读取流大小 1M
|
||||||
*/
|
*/
|
||||||
int INPUT_STREAM_SIZE = 1024;
|
int INPUT_STREAM_SIZE = 1048576;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* uEditor action
|
* uEditor action
|
||||||
|
@ -185,40 +185,49 @@ public class FileServiceImpl extends AbstractService implements IFileService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void downLoadFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> params) throws FileException {
|
public void downLoadFile(HttpServletRequest request, HttpServletResponse response, Map<String, Object> params) throws FileException {
|
||||||
OutputStream outputStream = null;
|
FilePO filePO = fileDao.getFile(params);
|
||||||
FileInputStream fileInputStream = null;
|
if (null == filePO) {
|
||||||
try {
|
throw new SearchException("文件获取失败");
|
||||||
FilePO filePO = fileDao.getFile(params);
|
}
|
||||||
if (null == filePO) {
|
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(filePO.getFilePath())));
|
||||||
throw new SearchException("文件获取失败");
|
OutputStream outputStream = response.getOutputStream();
|
||||||
}
|
) {
|
||||||
fileInputStream = new FileInputStream(new File(filePO.getFilePath()));
|
boolean isOpen = Boolean.valueOf(params.get("isOpen").toString());
|
||||||
response.setContentType(getContentType(filePO.getFileType()));
|
|
||||||
response.setHeader("Content-Length", filePO.getFileSize());
|
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"));
|
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filePO.getFileName(), "UTF-8"));
|
||||||
} 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"));
|
||||||
}
|
}
|
||||||
setRangeHeader(request, response, filePO.getFileId(), Long.valueOf(filePO.getFileSize()));
|
String rangeString = request.getHeader("Range");
|
||||||
outputStream = response.getOutputStream();
|
LOG.debug("range: {}", rangeString);
|
||||||
for (byte[] buf = new byte[INPUT_STREAM_SIZE]; fileInputStream.read(buf) > -1; ) {
|
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);
|
outputStream.write(buf, 0, buf.length);
|
||||||
|
outputLength += readLength;
|
||||||
}
|
}
|
||||||
outputStream.flush();
|
outputStream.flush();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new FileException("文件下载异常", e);
|
throw new FileException("文件输出异常");
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
if (null != outputStream) {
|
|
||||||
outputStream.close();
|
|
||||||
}
|
|
||||||
if (null != fileInputStream) {
|
|
||||||
fileInputStream.close();
|
|
||||||
}
|
|
||||||
} catch (Exception e1) {
|
|
||||||
throw new FileException("流关闭异常", e1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -230,18 +239,11 @@ public class FileServiceImpl extends AbstractService implements IFileService {
|
|||||||
* @param fileId
|
* @param fileId
|
||||||
* @param contentLength
|
* @param contentLength
|
||||||
*/
|
*/
|
||||||
private void setRangeHeader(HttpServletRequest request, HttpServletResponse response, String fileId, long contentLength) {
|
private void setRangeHeader(long startRange, long endRange, HttpServletResponse response, String fileId, long contentLength) {
|
||||||
if (request == null) {
|
response.setHeader("Content-Range", String.format("bytes %d-%d/%d", startRange, endRange, contentLength));
|
||||||
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("Accept-Ranges", "bytes");
|
||||||
response.setHeader("Etag", fileId);
|
response.setHeader("Etag", fileId);
|
||||||
|
response.setStatus(206);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -37,10 +37,14 @@ public class ResponseAdvice {
|
|||||||
@ResponseBody
|
@ResponseBody
|
||||||
@ExceptionHandler({SystemException.class, SQLSyntaxErrorException.class, Exception.class})
|
@ExceptionHandler({SystemException.class, SQLSyntaxErrorException.class, Exception.class})
|
||||||
public void responseException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException {
|
public void responseException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException {
|
||||||
LOG.error(e.getMessage(), e);
|
|
||||||
if (e instanceof InsufficientAuthenticationException) {
|
if (e instanceof InsufficientAuthenticationException) {
|
||||||
throw new InsufficientAuthenticationException(e.getMessage());
|
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(), "系统错误");
|
ErrorResult result = new ErrorResult(ErrorResultCodeEnum.SYSTEM_ERROR.getValue(), "系统错误");
|
||||||
if (e instanceof SaveException) {
|
if (e instanceof SaveException) {
|
||||||
result.setCode(ErrorResultCodeEnum.SAVE_ERROR.getValue());
|
result.setCode(ErrorResultCodeEnum.SAVE_ERROR.getValue());
|
||||||
|
Loading…
Reference in New Issue
Block a user