阿里雲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');

如此來請求便可.對象

相關文章
相關標籤/搜索