springboot 下载远程oss地址文件,生成zip包

文章介绍了如何使用Java编写RESTfulAPI,分别处理单条公告的下载,以及多选公告文件的Zip包下载,涉及到URL连接、流处理和PDF转换技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /**
     * @param
     * @param response
     * @功能描述 单条公告下载
     */
    @ApiOperation("单条公告下载")
    @Log(title = "单条公告下载", businessType = BusinessType.OTHER)
    @PostMapping("/download")
    public void download(String remoteFilePath, HttpServletResponse response) {

        log.info(" list list ");
        //Pdf2HtmlUtils.pdf2html("D:\\pdf2htmlEX-v1.0\\pdf2htmlEX.exe","D:\\pdf2htmlEX-v1.0\\PDF\\test2.pdf","D:\\pdf2htmlEX-v1.0\\HTML","my22.html");
        URL url = null;
        try {
            url = new URL(remoteFilePath);
            URLConnection conn = null;
            conn = url.openConnection();
            InputStream inputStream = conn.getInputStream();
            response.reset();
            response.setContentType(conn.getContentType());
            String filename="filename1.pdf";
            if (false) {
                // 在线打开方式 文件名应该编码成UTF-8
                response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(filename, "UTF-8"));
            } else {
                //纯下载方式 文件名应该编码成UTF-8
                response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            }

            byte[] buffer = new byte[1024];
            int len;
            OutputStream outputStream = response.getOutputStream();
            while ((len = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, len);
            }
            inputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 下载zip 包
     * @param
     * @param
     * @param response
     * @throws IOException
     */
    @ApiOperation("多选下载zip包")
    @Log(title = "多选下载zip包", businessType = BusinessType.OTHER)
    @PostMapping("/downloadZipFile")
    public  void downloadZipFile(String ossIds, HttpServletResponse response) throws IOException {
        //List<String> fileList=Arrays.asList("E:\\google_down\\688793\\688711公告.PDF","E:\\google_down\\688793\\688793公告.PDF");
        //List<String> fileList=Arrays.asList("https://static.cninfo.com.cn/finalpage/2024-01-31/1219044841.PDF","https://static.cninfo.com.cn/finalpage/2024-01-31/1219044842.PDF");
        //List<String> fileUrlList = null;
        //List<String> fileNameList = null;
        OssMetaGq ossMetaGq=new OssMetaGq();
        ossMetaGq.setOssIds(ossIds);
        List<OssMetaGq> ossMetaGqList = ossMetaGqService.selectJwOssMetaGqListDownZipSql(ossMetaGq);
        String zipFileName="batch_down_announcement.zip";
        byte[] buf = new byte[1024];
        BufferedOutputStream bos = null;
        ZipOutputStream out = null;
        try {
            bos = new BufferedOutputStream(response.getOutputStream());
            //响应到浏览器
            response.reset();
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-disposition","attachment;filename="+zipFileName);
            out = new ZipOutputStream(bos);
            for (int i=0;i<ossMetaGqList.size();i++){
                //File file = new File(filePathName);
                //FileInputStream in = new FileInputStream(file);
                OssMetaGq ossMetaGqOne = ossMetaGqList.get(i);
                String filePathName = ossMetaGqOne.getUrl();
                String pdate = DateUtils.dateTime(ossMetaGqOne.getPublishdate());
                String fileName = ossMetaGqOne.getStockcode()+" "+ossMetaGqOne.getStockticker()+" "+pdate+" "+ossMetaGqOne.getTitle()+".pdf";
                URL url = new URL(filePathName);
                URLConnection conn =  url.openConnection();
                InputStream in = conn.getInputStream();
                out.putNextEntry(new ZipEntry(fileName));
                int len = -1;
                while ((len=in.read(buf))!=-1){
                    out.write(buf,0,len);
                }
                in.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (out!=null)
                out.close();
            if (bos!=null)
                bos.close();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值