阿里云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文件下载设置正确,不报错也没法下载文件java

1.必须是get请求ajax

2.必须是同步请求浏览器

因此在app

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

时必须使用上面的写法异步

不要使用ajax异步请求,并且在ajax中设置为get是无论用的阿里云

使用code

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

如此来请求便可.对象

相关文章
相关标签/搜索