FileUtil.java 3.0 KB
Newer Older
苗卫卫 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
package com.starcharge.wios.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StringUtils;

public class FileUtil {
    /**
     * 下载static/templates/的文件
     *
     * @param response
     * @param fileName
     * @throws Exception
     */
    public static void downloadTemplate(HttpServletResponse response, String fileName) throws Exception {
        ClassPathResource resource = new ClassPathResource("static/templates/" + fileName);
        InputStream inputStream = resource.getInputStream();
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso8859-1"));
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = response.getOutputStream();
        
        int count = inputStream.available();
        byte[] bytes = new byte[count];
        inputStream.read(bytes, 0, count);
        inputStream.close();
        
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    }
    
    /**
     * 通过url下载
     *
     * @param response
     * @param urlStr
     * @throws Exception
     */
    public static void downFile(HttpServletResponse response, String urlStr) throws Exception {
        try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
            if (StringUtils.isEmpty(urlStr)) {
                return;
            }
            // 1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
            response.setContentType("multipart/form-data");
            // 2.设置文件头:最后一个参数是设置下载文件名
            response.setHeader("Content-Disposition", "attachment;fileName=download.zip");
            String[] resourceUrls = urlStr.split(",");
            for (String singleUrl : resourceUrls) {
                String[] urlStrArr = singleUrl.split("\\?");
                String[] linkArr = urlStrArr[0].split("\\/");
                String fileName = linkArr[linkArr.length - 1];
                zos.putNextEntry(new ZipEntry(fileName));
                URL url = new URL(singleUrl);
                HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
                httpConn.connect();
                InputStream cin = httpConn.getInputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = cin.read(buffer)) != -1) {
                    zos.write(buffer, 0, len);
                }
                cin.close();
            }
            zos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}