阿里云OSS文件下载设置正确,不报错也无法下载文件的问题解决方案

 public void download(HttpServletResponse response, String fileName, String businessName, String filedir1) throws IOException {
        try {
            filedir = filedir1;
            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            //获取fileid对应的阿里云上的文件对象
            OSSObject ossObject = getOssClient().getObject(bucketName, filedir + fileName);

            // 读去Object内容  返回
            BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());


            BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
            //通知浏览器以附件形式下载
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
            //BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(new File("f:\\a.txt")));

            byte[] car = new byte[1024];
            int L = 0;
            while ((L = in.read(car)) != -1) {
                out.write(car, 0, L);

            }
            if (out != null) {
                out.flush();
                out.close();
            }
            if (in != null) {
                in.close();
            }

            ossClient.shutdown();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

阿里云OSS文件下载设置正确,不报错也无法下载文件

1.必须是get请求

2.必须是同步请求

所以在

@RequestMapping(value = "/downloadTemplate",method = RequestMethod.GET)

时必须使用上面的写法

不要使用ajax异步请求,而且在ajax中设置为get是不管用的

使用

window.open("/fileUpload/downloadTemplate?fileName="+$('#template').val(),'top');

如此来请求即可.

转载于:https://my.oschina.net/u/3683339/blog/1574270

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值