修改文件下载找不到文件阻塞线程

This commit is contained in:
wenc000 2019-08-26 16:05:12 +08:00
parent 4acdf5ffaa
commit cee2b0bdca
3 changed files with 10 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package com.cm.common.plugin.controller.routes.file;
import com.cm.common.base.AbstractController;
import com.cm.common.config.properties.FileProperties;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.FileException;
import com.cm.common.exception.ParamsException;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.service.file.IFileService;
@ -100,7 +101,7 @@ public class FileRouteController extends AbstractController {
})
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@GetMapping("downloadfile/{isOpen}/{fileId}")
public void downLoadFile(HttpServletResponse response, @PathVariable("isOpen") String isOpen, @PathVariable("fileId") String fileId) throws ParamsException, SearchException {
public void downLoadFile(HttpServletResponse response, @PathVariable("isOpen") String isOpen, @PathVariable("fileId") String fileId) throws FileException, ParamsException {
if (!ISystemConstant.IS_TRUE.equals(isOpen) && !ISystemConstant.IS_FALSE.equals(isOpen)) {
throw new ParamsException("参数错误");
}

View File

@ -2,6 +2,7 @@ package com.cm.common.plugin.service.file;
import com.alibaba.fastjson.JSONObject;
import com.cm.common.enums.UploadTypeEnum;
import com.cm.common.exception.FileException;
import com.cm.common.exception.SearchException;
import com.cm.common.exception.base.SystemException;
import com.cm.common.pojo.dtos.FileDTO;
@ -113,9 +114,9 @@ public interface IFileService {
*
* @param response
* @param params
* @throws SearchException
* @throws FileException
*/
void downLoadFile(HttpServletResponse response, Map<String, Object> params) throws SearchException;
void downLoadFile(HttpServletResponse response, Map<String, Object> params) throws FileException;
/**
* 百度富文本编辑器

View File

@ -120,7 +120,7 @@ public class FileServiceImpl extends AbstractService implements IFileService {
}
@Override
public void downLoadFile(HttpServletResponse response, Map<String, Object> params) throws SearchException {
public void downLoadFile(HttpServletResponse response, Map<String, Object> params) throws FileException {
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
try {
@ -128,19 +128,20 @@ public class FileServiceImpl extends AbstractService implements IFileService {
if (null == filePO) {
throw new SearchException("文件获取失败");
}
fileInputStream = new FileInputStream(new File(filePO.getFilePath()));
response.setContentType(getContentType(filePO.getFileType()));
response.addHeader("Content-Length", filePO.getFileSize());
response.setHeader("Content-Length", filePO.getFileSize());
if (!Boolean.valueOf(params.get("isOpen").toString())) {
response.addHeader("Content-Disposition", "attachment;fileName=" + filePO.getFileName());
response.setHeader("Content-Disposition", "attachment;fileName=" + filePO.getFileName());
}
outputStream = response.getOutputStream();
fileInputStream = new FileInputStream(new File(filePO.getFilePath()));
for (byte[] buf = new byte[INPUT_STREAM_SIZE]; fileInputStream.read(buf) > -1; ) {
outputStream.write(buf, 0, buf.length);
}
outputStream.flush();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new FileException("文件下载异常");
} finally {
try {
if (null != outputStream) {
@ -152,7 +153,6 @@ public class FileServiceImpl extends AbstractService implements IFileService {
} catch (Exception e1) {
LOG.error(e1.getMessage(), e1);
}
throw new SearchException("文件获取失败");
}
}