阿里云oss多文件批量获取打成zip包下载详解

背景:因为阿里云文件下载sdk中只有单个文件下载教程,但是工作中多文件批量的下载方式还是比较多的。于是,查看了许多相关博客和资料,自己搭建了个小demo,经过测试,运行正常。

 

步骤:

 

1:pom.xml中添加maven依赖:

 

 

     com.aliyun.oss

     aliyun-sdk-oss

     2.8.3

2:主要方法代码:

 

@RequestMapping("/api/downlownd")

    public StringgetOssFile(HttpServletRequest request, HttpServletResponse response){

 

       // endpoint以杭州为例,其它region请按实际情况填写,1改为自己的

       String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";

       // 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录https://ram.console.aliyun.com 创建

       String accessKeyId = "2改为自己的";

       String accessKeySecret = "3改为自己的";

       String bucketName = "4改为自己的";

       //要下载的文件名(ObjectName)字符串,中间用‘,’间隔。文件名从bucket目录开始.5改为自己的

       String key ="focus/item/P8c5z8BKWY1533108107411.png,focus/item/fJ4WmmbM5w1533108018272.png";

       try {

           // 初始化

           OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);;

           //6改为自己的名称

           String fileName = "test.zip";

           // 创建临时文件

           File zipFile = File.createTempFile("test", ".zip");

           FileOutputStream f = new FileOutputStream(zipFile);

           /**

            * 作用是为任何OutputStream产生校验和

            * 第一个参数是制定产生校验和的输出流,第二个参数是指定Checksum的类型 (Adler32(较快)和CRC32两种)

            */

           CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());

           // 用于将数据压缩成Zip文件格式

           ZipOutputStream zos = new ZipOutputStream(csum);

 

           String[] keylist = key.split(",");

           for (String ossfile : keylist) {

               // 获取Object,返回结果为OSSObject对象

               OSSObject ossObject = ossClient.getObject(bucketName, ossfile);

               // 读去Object内容  返回

               InputStream inputStream = ossObject.getObjectContent();

               // 对于每一个要被存放到压缩包的文件,都必须调用ZipOutputStream对象的putNextEntry()方法,确保压缩包里面文件不同名

 

               zos.putNextEntry(new ZipEntry(ossfile.split("/")[2]));

               int bytesRead = 0;

               // 向压缩文件中输出数据

               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("+", "+");   //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();

       }

       return null;

    }

注意:这段代码有5处需要改动为自己阿里云上的配置。分别是endpoint,accessKeyId,accessKeySecret ,bucketName,key,fileName。

 

前四个都是阿里云上的自己配置的,复制过来就好。第五个是要批量下载的文件集字符串(单个文件名文件名从bucket目录开始),第六个是下载打包成zip的文件名。这只是简单的测试,要想用在生产上需要好好整理下代码。

 

你可能感兴趣的:(阿里云oss多文件批量获取打成zip包下载详解)