java package compressed into a plurality of files and downloading the output stream

pom.xml

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.18</version>
        </dependency>

 

// packaged into a plurality of disk files and compressed output stream downloaded 
    @GetMapping (value = "/ Download1" )
     public  void Download1 (the HttpServletRequest Request, the HttpServletResponse Response) throws Exception {
        String outputFileName = "文件" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";
        // 设置response参数
        response.reset();
        response.setContentType("content-type:octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String((outputFileName).getBytes(), "iso-8859-1"));
        ServletOutputStream out = response.getOutputStream();

        ZipArchiveOutputStream zous = new ZipArchiveOutputStream(out);
        zous.setUseZip64(Zip64Mode.AsNeeded);

        File f1 = new File("D:\\testfile\\aaa.png");
        File f2 = new File("D:\\testfile\\bbb.png");
        List<File> fileList = new ArrayList<>();
        fileList.add(f1);
        fileList.add(f2);

        for (File file : fileList) {
            String fileName = file.getName();
            InputStream inputStream = new FileInputStream(file);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            if (baos != null) {
                baos.flush();
            }
            byte[] bytes = baos.toByteArray();

            // set the file name 
            ArchiveEntry entry = new new ZipArchiveEntry (fileName);
            zous.putArchiveEntry(entry);
            zous.write(bytes);
            zous.closeArchiveEntry();
            if (baos != null) {
                baos.close();
            }
        }
        if(zous!=null) {
            zous.close();
        }
    }

    // packaged url plurality of network resource files into the file output stream compressed and download 
    @GetMapping (value = "/ Download2" )
     public  void Download2 (the HttpServletRequest Request, the HttpServletResponse Response) throws Exception {
        String outputFileName = "文件" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";
        // 设置response参数
        response.reset();
        response.setContentType("content-type:octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String((outputFileName).getBytes(), "iso-8859-1"));
        ServletOutputStream out = response.getOutputStream();

        ZipArchiveOutputStream zous = new ZipArchiveOutputStream(out);
        zous.setUseZip64(Zip64Mode.AsNeeded);

        String path1 = "http://www.baidu.com/img/bd_logo1.png";
        String path2 = "http://www.baidu.com/img/bd_logo1.png";
        List<String> pathList = new ArrayList<>();
        pathList.add(path1);
        pathList.add(path2);

        for (String path : pathList) {
            String fileName = UUID.randomUUID() + ".png";
            InputStream inputStream = getInputStreamFromUrl(path);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            if (baos != null) {
                baos.flush();
            }
            byte[] bytes = baos.toByteArray();

            // set the file name 
            ArchiveEntry entry = new new ZipArchiveEntry (fileName);
            zous.putArchiveEntry(entry);
            zous.write(bytes);
            zous.closeArchiveEntry();
            if (baos != null) {
                baos.close();
            }
        }
        if(zous!=null) {
            zous.close();
        }
    }

    /**
     * Get file InputStream through Network Address
     *
     * @Param path address
     * @return
     */
    public static InputStream getInputStreamFromUrl(String path) {
        URL url = null;
        InputStream is = null;
        try {
            url = new URL(path);
        } catch (MalformedURLException e) {
            e.printStackTrace ();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.connect();
            is = conn.getInputStream();
        } catch (IOException e) {
            e.printStackTrace ();
        }
        return is;
    }

 

Guess you like

Origin www.cnblogs.com/zengnansheng/p/11649704.html