使用httpclient进行文件下载

使用httpclient进行文件下载获取文件名获取方式

    先从返回头的content-Disposition中获取,如果没有再从下载的Url中获取。

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class FilesUtils {

    private final static Pattern pattern = Pattern.compile(".*fileName=(.*)");

    /**
     * 文件下载
     *
     * @param httpUrl 下载的url
     * @return
     */
    public static FileMetaInfoDTO downLoadFromUrl(String httpUrl) throws IOException {
        File file = null;

        FileMetaInfoDTO fileMetaInfoDTO = null;
        try {
            // 统一资源
            URL url = new URL(httpUrl);
            // 连接类的父类,抽象类
            URLConnection urlConnection = url.openConnection();
            // http的连接类
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
            // 设定请求的方法,默认是GET
            httpURLConnection.setRequestMethod("GET");
            // 设置字符编码
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            urlConnection.setConnectTimeout(MccConfigUtil.getDonwLoadTimeOut());
            // 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
            httpURLConnection.connect();

            String suffix  = getSuffix(httpUrl);
            if (StringUtils.isEmpty(suffix)) {
                suffix = ".tmp";
            }
            file = File.createTempFile("httpdonwFile", suffix);
            BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());

            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            FileOutputStream out = new FileOutputStream(file);
            int size = 0;
            int len = 0;
            byte[] buf = new byte[1024];
            while ((size = bin.read(buf)) != -1) {
                len += size;
                out.write(buf, 0, size);
            }
            bin.close();
            out.close();
            String fileName = tryGetFileName(urlConnection);
            fileMetaInfoDTO = new FileMetaInfoDTO(fileName, file);
        } catch (MalformedURLException e) {
            log.error("downLoad file eror url = {} ", httpUrl, e);
        } catch (IOException e) {
            log.error("downLoad file eror url = {} ", httpUrl, e);
        } finally {
            return fileMetaInfoDTO;
        }
    }

    private static String tryGetFileName(URLConnection conn) {

        String contentDisposition = null;
        try {
            String disposition = conn.getHeaderField("content-Disposition");
            if (!StringUtils.isEmpty(disposition)) {
                contentDisposition = URLDecoder.decode(disposition, "UTF-8");
                Matcher matcher = pattern.matcher(contentDisposition);
                return matcher.group(1);
            } else {
                String path = conn.getURL().getPath();
                Integer start = path.lastIndexOf('/') + 1;
                String fileName = path.substring(start);
                return URLDecoder.decode(fileName, "UTF-8");
            }
        } catch (UnsupportedEncodingException e) {
            log.error("文件名解析失败 url = {}", conn.getURL(), e);
        } catch (Exception e) {
            log.error("文件名解析失败 url = {}", conn.getURL(), e);
        }
        return null;
    }

    /**
     * 从输入流中获取字节数组
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }
    
    public static String getSuffix(String path) {
        String suffix = StringUtils.substringAfterLast(path, ".");
        suffix = StringUtils.substringBeforeLast(suffix, "?");
        suffix = StringUtils.substringBeforeLast(suffix, "@");
        return "." + suffix;
    }

@Data
public static class FileMetaInfoDTO  {
    String fileName;
    File file;

    public FileMetaInfoDTO(String fileName, File file) {
        this.fileName = fileName;
        this.file = file;
    }

    public FileMetaInfoDTO() {
    }
}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值