网络文件获取到服务器后,经服务器处理后响应给前端(接口地址就是预览或下载地址,流直接交给浏览器)
import cn.hutool.http.HttpUtil;
@GetMapping("/netDownLoadNet/{id}")
public void netDownLoadNet(@PathVariable("id") Long id, HttpServletResponse response) throws Exception {
TBAttachmentDTO tbAttachmentDTO = tBAttachmentService.get(id);
byte[] bytes = HttpUtil.downloadBytes(tbAttachmentDTO.getFileUrl());
response.reset();
response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(tbAttachmentDTO.getName(), "UTF-8"));
OutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes);
ouputStream.flush();
ouputStream.close();
}
网络文件获取到服务器后,经服务器处理后响应给前端(接口返回流给前端,前端接收流处理预览或下载)
@RequestMapping("/netDownLoadNet")
public ResponseEntity<byte[]> netDownLoadNet(String netAddress, String filename, boolean isOnLine, HttpServletResponse response)
throws Exception {
byte[] bytes = HttpUtil.downloadBytes(netAddress);
//设置响应头
HttpHeaders headers = new HttpHeaders();
//通知浏览器以下载的方式打开文件 //inline attachment
headers.setContentDispositionFormData("inline", filename);
//定义以流的形式下载返回文件数据
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
}