最近的一个需求需要根据一个mp3的链接获取文件流上传到内部的存储平台,
记录一下获取文件流的代码吧。
pom文件

<dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.8.0</version>
        </dependency>

代码:

package com.example.demo.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.ByteArrayBuffer;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.net.ssl.SSLContext;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

/**
 * @Author:luobinghan
 */
@Service
@Slf4j
public class HttpService {

    private int maxConnections = 500;
    private int maxConnectionsPerRoute = 100;
    private static final int DEFAULT_BUFFER_SIZE = 4096;
    private CloseableHttpClient httpStreamClient;

    public HttpService() {
        try {
            this.httpStreamClient = HttpClients.custom().setConnectionManager(getManager(getSslSocketFactory())).disableAutomaticRetries().build();

        } catch (Exception e) {
            log.error("init httpStreamClient error");
        }
    }

    /**
     * 获取url链接内容
     * @param url
     * @return
     */
    public ByteBuffer getUrl(String url) {
        HttpEntity entity = null;
        try {
            entity = httpGet(url);
        } catch (Exception e) {
            return null;
        }
        try {
            ByteBuffer bytes = getBytesFromHttpRsp(entity);
            return bytes;
        } catch (Exception e) {
            return null;
        }
    }

    private PoolingHttpClientConnectionManager getManager(LayeredConnectionSocketFactory sslSocketFactory) {
        ConnectionSocketFactory plainSocketFactory = PlainConnectionSocketFactory.getSocketFactory();
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", plainSocketFactory)
                .register("https", sslSocketFactory).build();
        PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry);
        manager.setMaxTotal(this.maxConnections);
        manager.setDefaultMaxPerRoute(this.maxConnectionsPerRoute);
        return manager;
    }

    private LayeredConnectionSocketFactory getSslSocketFactory() throws Exception {
        SSLContext sslContext = SSLContexts.custom()
                .loadTrustMaterial((chain, authType) -> true).build();

        return new SSLConnectionSocketFactory(sslContext, new String[]
                {"SSLv2Hello", "SSLv3", "TLSv1","TLSv1.1", "TLSv1.2" }, null,
                NoopHostnameVerifier.INSTANCE);
    }

    private HttpEntity httpGet(String url) throws Exception {
        return httpStreamClient.execute(new HttpGet(url)).getEntity();
    }

    private ByteBuffer getBytesFromHttpRsp(org.apache.http.HttpEntity entity) throws Exception {
        final InputStream inputStream = entity.getContent();
        try {
            int capacity = (int)entity.getContentLength();
            if (capacity <= 0) {
                capacity = DEFAULT_BUFFER_SIZE;
            }
            final ByteArrayBuffer buffer = new ByteArrayBuffer(capacity);
            final byte[] tmp = new byte[DEFAULT_BUFFER_SIZE];
            int bytesRead;
            while((bytesRead = inputStream.read(tmp)) != -1) {
                buffer.append(tmp, 0, bytesRead);
            }
            return ByteBuffer.wrap(buffer.toByteArray());
        } finally {
            inputStream.close();
        }
    }

    public static void main(String[] args) {
        String url = "xxx";
        HttpService httpService = new HttpService();
        byte[] bytes = httpService.getUrl(url).array();
        InputStream inputStream =  new ByteArrayInputStream(bytes);
    }
}