Ali cloud packaged into a zip format, batch download

    / ** 
     * (download attachment) to download files from the cloud Ali zip download multiple files second 
     * @param Request 
     * @param the Response 
     * / 
    @ResponseBody 
    @ RequestMapping (value = "/ zipFilesDown", Method, = RequestMethod. GET) 
    public void zipFilesDown (Request the HttpServletRequest, HttpServletResponse the Response) { 
        String userId = request.getParameter ( "userId"); 
        // Ali cloud-based configuration 
        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; 
            // initialization 
        String fileHost = OSSConstants.OSS_FILE_HOST;
        {the try 
            OSSClient ossClient = new new OSSClient (Endpoint, accessKeyId, accessKeySecret) ;; 
            String fileName = userId + ".zip"; 
            // create temporary files 
            File the ZipFile = File.createTempFile (userId, ".zip"); 
            FileOutputStream f = new new a FileOutputStream (ZipFile); 
           
            CheckedOutputStream An Csum = new new CheckedOutputStream An (F, the Adler32 new new ()); 
            // for Zip file format as a data compression 
            the ZipOutputStream the ZipOutputStream new new = ZOS (Csum); 
 
            // configured ListObjectsRequest request. 
            ListObjectsRequest = new new ListObjectsRequest ListObjectsRequest (bucketName); 
            // list all files and directories under the userId folder.
            listObjectsRequest.setPrefix (fileHost + "/" + the userId + "/"); 
            ObjectListing Listing = ossClient.listObjects (listObjectsRequest); 
            // iterate through all files. 
            System.out.println ( "Objects:"); 
            for (OSSObjectSummary ossObjectSummary: listing.getObjectSummaries ()) { 
                System.out.println (ossObjectSummary.getKey ()); 
                String eachFileName = ossObjectSummary.getKey () the substring (ossObjectSummary.. getKey () lastIndexOf ( "-") +. 1);. 
                // Get Object, Object returns a value OSSObject 
                OSSObject ossObject = ossClient.getObject (bucketName, ossObjectSummary.getKey ()); 
                // read the contents returned to Object
                InputStream = ossObject.getObjectContent the InputStream ();  
                / / for each packet to be stored in the compressed file, you must call ZipOutputStream object putNextEntry () method, which ensure that compressed files with different names
                zos.putNextEntry (the ZipEntry new new (eachFileName)); 
                int bytesRead; 
                // the output data to the compressed file in 
                the while ((bytesRead InputStream.read = ()) = -. 1!) { 
                    ZOS. write (bytesRead); 
                } 
                inputStream.close (); 
                zos.closeEntry (); // current file written, writes the next item is positioned 
            } 
            zos.close (); 
            String header = request.getHeader ( "the User-- Agent ") .toUpperCase (); 
            IF (header.contains (" MSIE ") || header.contains (" TRIDENT ") || header.contains (" EDGE ")) { 
                fileName = the 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 (); 
        } 
    } 

To meet their development needs, they are more lazy easy, direct copy over the original author of the code recording it.

Original Reference: https://blog.csdn.net/m0_37844800/article/details/81068833

Guess you like

Origin www.cnblogs.com/yang-xiansen/p/11205255.html