zoukankan      html  css  js  c++  java
  • oss打包下载

        private static final String OSS_ENDPOINT = PropertiesUtil.getString("oss.endpoint");
    
        private static final String OSS_ACCESS_KEY = PropertiesUtil.getString("oss.access.key");
    
        private static final String OSS_SECRET = PropertiesUtil.getString("oss.secret");
    
        private static final String OSS_BUCKET_NAME = PropertiesUtil.getString("oss.bucket.name");
    
        // private static final String OSS_CT_BUCKET_NAME =
        // PropertiesUtil.getString("oss.bucket.ct.name");
    
        private static final OSSClient OSS_CLIENT = new OSSClient(OSS_ENDPOINT, OSS_ACCESS_KEY, OSS_SECRET);
    
    /**
         * 批量下载oss 文件 并打成zip 包 返回到response中
         *
         * @param fileNames
         *            oss上的文件名集合 如:product/image/3448275920.png  * @param zipFileName
         *            压缩包文件名
         * @param response
         *            HttpServletResponse
         */
        public static void batchDownLoadOssFile(List<String> fileNames, String zipFileName, HttpServletResponse response) {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            //要下载成什么类型的文件,这里直接加后缀
            response.setHeader("Content-Disposition", "attachment;fileName=" + zipFileName + ".zip");
            BufferedInputStream bis = null;
            try {
                ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
                for (String fileName : fileNames) {
                    Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
                    GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(OSS_BUCKET_NAME, fileName,
                            HttpMethod.GET);
                    // 设置过期时间。
                    request.setExpiration(expiration);
    //                request.setContentType("application/pdf");
                    // 生成签名URL(HTTP GET请求)。
                    URL signedUrl = OSS_CLIENT.generatePresignedUrl(request);
                    // 使用签名URL发送请求。
                    OSSObject ossObject = OSS_CLIENT.getObject(signedUrl, new HashMap<>());
                    if (ossObject != null) {
                        InputStream inputStream = ossObject.getObjectContent();
                        byte[] buffs = new byte[1024 * 10];
    
                        String zipFile = fileName.substring(fileName.lastIndexOf("/") + 1)+".pdf";
                        ZipEntry zipEntry = new ZipEntry(zipFile);
                        zos.putNextEntry(zipEntry);
                        bis = new BufferedInputStream(inputStream, 1024 * 10);
    
                        int read;
                        while ((read = bis.read(buffs, 0, 1024 * 10)) != -1) {
                            zos.write(buffs, 0, read);
                        }
                        ossObject.close();
                    }
                }
                zos.close();
                //关闭流
                IOUtil.close(bis);
            } catch (Exception e) {
                LOGGER.error("打包下载发生异常:",e);
            } finally {
                // 关闭流
                try {
                    response.getOutputStream().flush();
                    response.getOutputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    springboot框架,前端加一个接口

    @GetMapping(value = "/downloadBookZip")
        public void downloadBookZip(HttpServletRequest request,HttpServletResponse response) {
            String subjectIdstr = request.getParameter("subjectId");
            String className = request.getParameter("className");
            String studentNum = request.getParameter("studentNum");
            if(Strings.isNullOrEmpty(subjectIdstr)) {
                throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR, "必须有科目ID");
            }
            Long subjectId = Long.parseLong(subjectIdstr);
            //查询生成文件路径
            List<String> list = wrongTopicDao.queryWrongTopicOssKey(subjectId,className,studentNum);
    //上面按照个人业务查询自己的文件key即可 OssUtil.batchDownLoadOssFile(list,
    "books", response); }
  • 相关阅读:
    2017.10.20
    2017.10.14
    2017.10.19
    2017.10.18
    2017.10.17
    软件工程个人作业02
    构建之法读后感03
    构建之法阅读笔记02
    二柱子问题(随机产生四则运算题目)
    课后作业2(构建之法阅读计划)
  • 原文地址:https://www.cnblogs.com/Ebird/p/11733406.html
Copyright © 2011-2022 走看看