springboot下载文件

网络文件获取到服务器后,经服务器处理后响应给前端(接口地址就是预览或下载地址,流直接交给浏览器)

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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值