下载下来打开出现下面错误:
实现代码逻辑是:使用Dubbo的生产者消费者进行远程调用,服务器本地查询数据库生成excel在本地然后传给消费者的File,消费者接收到File然后进行下载操作。
方案一:未解决
@ApiOperation(value = "批量下载标注列表根据对象查询最多导出200条数据")
@RequestMapping(value = "/downloadEventTagProcess", method = RequestMethod.GET)
public CorpusResponse downloadEventTagProcessFile(EventTagProcessVo eventTagProcessVo, HttpServletResponse httpServletResponse) throws Exception {
Long nowTIme = System.currentTimeMillis();
CorpusResponse response = CorpusResponseBuilder.newSuccessResponse();
File scFileDir = eventTagProcessFacade.downloadInfoTemplate(eventTagProcessVo);
try {
String fileName = "标注列表" + scFileDir.getName();
fileName = URLEncoder.encode(fileName, "utf-8");
httpServletResponse.setHeader("Content-disposition", "attachment;filename=" + fileName);
httpServletResponse.setCharacterEncoding("utf-8");
httpServletResponse.setContentType("application/octet-stream;charset=utf-8");
InputStream inputStream = new FileInputStream(scFileDir);
OutputStream outputStream= httpServletResponse.getOutputStream();
IOUtils.copy(inputStream,outputStream);
inputStream.close();
outputStream.close();
} catch (IOException e) {
response = CorpusResponseBuilder.newErrorResponse(e);
}
Long nowTime = System.currentTimeMillis();
System.out.println("处理请求发生时长:" + (nowTIme - nowTime) / 1000);
return response;
}
方案2也没有解决问题所在:
if (scFileDir != null) {
//
// // 实现文件下载
// byte[] buffer = new byte[1024];
// FileInputStream fis = null;
// BufferedInputStream bis = null;
// try {
// // 配置文件下载
// httpServletResponse.setHeader("content-type", "application/octet-stream");
// httpServletResponse.setContentType("application/octet-stream");
// // 下载文件能正常显示中文
// httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(scFileDir.getName(), "UTF-8"));
// fis = new FileInputStream(scFileDir);
// bis = new BufferedInputStream(fis);
// OutputStream os = httpServletResponse.getOutputStream();
// int i = bis.read(buffer);
// while (i != -1) {
// os.write(buffer, 0, i);
// i = bis.read(buffer);
// }
// System.out.println("Download the song successfully!");
// }
// catch (Exception e) {
// System.out.println("Download the song failed!");
// }
// finally {
// if (bis != null) {
// try {
// bis.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// if (fis != null) {
// try {
// fis.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// }
方案三:也是如此。
@RequestMapping(value = "/downloadInfoTemplate", method = RequestMethod.GET)
public ResponseEntity<Object> downloadFile(HttpServletResponse httpServletResponse) throws FileNotFoundException {
File file= informationImportData.downloadInfoTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control","no-cache,no-store,must-revalidate");
headers.add("Pragma","no-cache");
headers.add("Expires","0");
ResponseEntity<Object> responseEntity = ResponseEntity.ok().headers(headers)
.contentLength(0)
.contentType(MediaType.parseMediaType("application/text")).body("文件模板不存在!请联系管理员!");
if(null!=file){
InputStreamResource resource = new InputStreamResource(new FileInputStream((file)));
headers.add("Content-Disposition",String.format("attachment;filename=\"%s\"",file.getName()));
responseEntity = ResponseEntity.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/text"))
.body(resource);
}
return responseEntity;
}
主要解决办法是在于前端Vue接受之后进行读取流生成文件就可以的