FeignClient调用内部服务下载文件错误打开方式

文章详细描述了Java中如何通过controller层的GetMapping接口处理文件下载请求,涉及service接口的实现,包括流操作和异常处理,以及FeignClient的错误定义。调用示例中的问题导致无法下载数据但不报错。
摘要由CSDN通过智能技术生成

文件服务器提供的接口

controller层

/**
 * 绝对路径-文件下载
 */
@GetMapping("/absolutePathDownload")
public void absolutePathDownload(String fileName, HttpServletResponse response) {
    sysFileService.absolutePathDownload(fileName, response);
}

service接口层 

    /**
     * 绝对路径-文件下载
     *
     * @param fileName 文件路径名称
     * @param response 返回值
     */
    void absolutePathDownload(String fileName, HttpServletResponse response);

impl实现层

 @Override
    public void absolutePathDownload(String fileName, HttpServletResponse response) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            // 获取文件对象
            inputStream = XXX
            byte buf[] = new byte[1024];
            int length = 0;
            response.reset();
            response.setHeader("Content-Disposition", "attachment;filename=" +
                    URLEncoder.encode(fileName.substring(fileName.lastIndexOf("/") + 1), "UTF-8"));
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            // 输出文件
            while ((length = inputStream.read(buf)) > 0) {
                outputStream.write(buf, 0, length);
            }
            logger.info("下载成功");
            inputStream.close();
        } catch (Throwable ex) {
            logger.error("下载文件失败:{}", ex);
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            String data = "文件下载失败";
            try {
                OutputStream ps = response.getOutputStream();
                ps.write(data.getBytes("UTF-8"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        } finally {
            try {
                outputStream.close();
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
FeignClient接口错误定义
 /**
     * 绝对路径-下载文件
     *
     * @param
     * @return 结果
     */
    @GetMapping(value = "/absolutePathDownload")
    void absolutePathDownload(@RequestParam("fileName") String fileName, @RequestHeader("response") HttpServletResponse response);

错误调用

    @PostMapping("/download")
    public void download(@RequestBody ChuanId chuanId, HttpServletResponse response) {
        if (chuanId != null && StringUtils.isNotEmpty(chuanId.getId())) {
            QualityRequire qualityRequire = qualityRequireService.selectQualityRequireByDemandNo(chuanId.getId());
            String downloadPath = qualityRequire.getAnnexPath();
            String replace = downloadPath.replace(domain + prefix, path);
            fileService.absolutePathDownload(replace, response);
        }
    }

这种调用是得不到下载数据的,但也不会报错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值