• 阿里云打包成zip格式,批量下载


        /**
         * 从阿里云下载文件 (以附件形式下载) 多个文件进行zip下载  第二种
         * @param request
         * @param response
         */
        @ResponseBody
        @RequestMapping(value = "/zipFilesDown", method = RequestMethod.GET)
        public void zipFilesDown(HttpServletRequest request, HttpServletResponse response){
            String userId = request.getParameter("userId");
            //阿里云基础配置
            String endpoint = OSSConstants.OSS_ENDPOINT;
            String accessKeyId = OSSConstants.OSS_ACCESS_KEY_ID;
            String accessKeySecret = OSSConstants.OSS_ACCESS_KEY_SERCRET;
            String bucketName = OSSConstants.OSS_BUCKET_NAME_ONE;
            String fileHost = OSSConstants.OSS_FILE_HOST;
            try {
                // 初始化
                OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);;
                String fileName = userId + ".zip";
                // 创建临时文件
                File zipFile = File.createTempFile(userId, ".zip");
                FileOutputStream f = new FileOutputStream(zipFile);
               
                CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
                // 用于将数据压缩成Zip文件格式
                ZipOutputStream zos = new ZipOutputStream(csum);
     
                // 构造ListObjectsRequest请求。
                ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
                // 列出userId目录下的所有文件和文件夹。
                listObjectsRequest.setPrefix(fileHost + "/" + userId + "/");
                ObjectListing listing = ossClient.listObjects(listObjectsRequest);
                // 遍历所有文件。
                System.out.println("Objects:");
                for (OSSObjectSummary ossObjectSummary : listing.getObjectSummaries()) {
                    System.out.println(ossObjectSummary.getKey());
                    String eachFileName = ossObjectSummary.getKey().substring(ossObjectSummary.getKey().lastIndexOf("-")+1);
                    // 获取Object,返回结果为OSSObject对象
                    OSSObject ossObject = ossClient.getObject(bucketName, ossObjectSummary.getKey());
                    // 读去Object内容  返回
                    InputStream inputStream = ossObject.getObjectContent();
                    // 对于每一个要被存放到压缩包的文件,都必须调用ZipOutputStream对象的putNextEntry()方法,确保压缩包里面文件不同名
                    zos.putNextEntry(new ZipEntry(eachFileName));
                    int bytesRead;
                    // 向压缩文件中输出数据
                    while((bytesRead=inputStream.read())!=-1){
                        zos.write(bytesRead);
                    }
                    inputStream.close();
                    zos.closeEntry(); // 当前文件写完,定位为写入下一条项目
                }
                zos.close();
                String header = request.getHeader("User-Agent").toUpperCase();
                if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
                    fileName = URLEncoder.encode(fileName, "utf-8");
                    fileName = fileName.replace("+", "%20");    //IE下载文件名空格变+号问题
                } else {
                    fileName = new String(fileName.getBytes(), "ISO8859-1");
                }
                response.reset();
                response.setContentType("text/plain");
                response.setContentType("application/octet-stream; charset=utf-8");
                response.setHeader("Location", fileName);
                response.setHeader("Cache-Control", "max-age=0");
                response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
     
                FileInputStream fis = new FileInputStream(zipFile);
                BufferedInputStream buff = new BufferedInputStream(fis);
                BufferedOutputStream out=new BufferedOutputStream(response.getOutputStream());
                byte[] car=new byte[1024];
                int l=0;
                while (l < zipFile.length()) {
                    int j = buff.read(car, 0, 1024);
                    l += j;
                    out.write(car, 0, j);
                }
                // 关闭流
                fis.close();
                buff.close();
                out.close();
     
                ossClient.shutdown();
                // 删除临时文件
                zipFile.delete();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } 

    满足了自己的开发需求,本人比较偷懒省事,直接把原作者的代码copy过来记录一下。

    参考原文:https://blog.csdn.net/m0_37844800/article/details/81068833

    当你的才华还撑不起你的野心时
    那你就应该静下心来学习
    当你的能力还驾驭不了你的目标时
    那就应该沉下心来历练
  • 相关阅读:
    css 伪类
    tornado 作业 简单首页 登录页 个人中心
    tornado 作业 自定义模板 UIMethod以UIModule
    tornado 网页提交内容 展示内容作业
    tornado
    javascript
    廖雪峰官网学习js 数组
    廖雪峰官网学习js 字符串
    廖雪峰官网学习js 数据类型和变量
    协程gevent学习
  • 原文地址:https://www.cnblogs.com/yang-xiansen/p/11205255.html
Copyright © 2020-2023  润新知