Http Download File
package com.augmentum.mediaprocess.util; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.fluent.Request; import org.apache.http.client.utils.URIUtils; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URI; @Slf4j public class HttpUtil { public static InputStream downFile(String src) throws IOException { return downFile(URI.create(src)); } /** * Download File from web */ public static InputStream downFile(URI uri) throws IOException { HttpResponse httpResponse; try { Request request = Request.Get(uri); HttpHost httpHost = URIUtils.extractHost(uri); if (StringUtils.isNotEmpty(httpHost.getHostName())) { request.setHeader("Host", httpHost.getHostName()); } request.addHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); httpResponse = request.execute().returnResponse(); } catch (Exception e) { log.error("Remote request failed,url=" + uri, e); throw new FileNotFoundException(); } int code = httpResponse.getStatusLine().getStatusCode(); if (code != 200) { throw new FileNotFoundException(); } return httpResponse.getEntity().getContent(); } }
相关推荐
houjinkai 2020-03-01
xiaouncle 2020-01-23
gaoyubotaili 2014-06-18
心丨悦 2019-12-24
luofuIT成长记录 2019-12-15
Lincain 2019-11-16
killmice 2016-07-14
GoAheadY 2011-07-18
RickyHuo成长之路 2012-06-20
xiyf0 2018-05-30
yongzhang 2019-05-24
AbitGo 2019-04-12
haoxun0 2019-04-12
finnaxu 2016-04-29
87921432 2016-03-15
GimmeS 2016-03-01
LiTOPPPP 2015-06-09
QAnyang 2013-10-08
BigPig 2013-05-20