日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

httpclient 下载文件

發(fā)布時(shí)間:2023/12/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 httpclient 下载文件 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這里寫自定義目錄標(biāo)題

  • pom.xml
  • download

pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.go</groupId><artifactId>tool-com</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.25</version></dependency><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.25</version><!-- <scope>test</scope> --></dependency><!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.13</version></dependency><!-- https://mvnrepository.com/artifact/commons-lang/commons-lang --><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.0</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.3</version></dependency></dependencies></project>

download

package cn.go;import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils;import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream;public class HttpClientTest {public static void main(String[] args) {try {download("6010102080720200036802");} catch (IOException e) {e.printStackTrace();}/* try {File file = new File("d:/policyNo_list.txt");BufferedReader reader = new BufferedReader(new FileReader(file));String policyNo;while ((policyNo = reader.readLine()) != null) {System.out.println(policyNo);download(policyNo);}} catch (IOException e) {e.printStackTrace();}*/}private static void download(String str) throws IOException {String urlPrefix = "http://xxx";String url = urlPrefix + "/xxx?policyNo=" + str;String result= null;String defaultCharset = "utf-8";DefaultHttpClient httpclient = new DefaultHttpClient();HttpGet httpGet = new HttpGet(url);HttpResponse response = httpclient.execute(httpGet);if (response != null) {HttpEntity resEntity = response.getEntity();if (resEntity != null) {result = EntityUtils.toString(resEntity, defaultCharset);}}System.out.println(result);// http://ip:port//planCode = code + "car_"+ planCode + ".ftl";String fileUrl = urlPrefix + "/xxxx"+result;System.out.println(fileUrl);HttpGet httpGetFile = new HttpGet(fileUrl);HttpResponse fileResponse = httpclient.execute(httpGetFile);if (fileResponse != null) {HttpEntity resEntity = fileResponse.getEntity();Header contentType = fileResponse.getEntity().getContentType();System.out.println(contentType);if (resEntity != null) {result = EntityUtils.toString(resEntity, defaultCharset);System.out.println(result);if(!"xxxx".equals(result)){System.exit(1);}return;}}InputStream inputStream = fileResponse.getEntity().getContent();ByteArrayOutputStream byteArray = new ByteArrayOutputStream();byte[] buff = new byte[1024*1024]; //如果是稍微大的文件,這里配置的大一些int len = 0;while((len = inputStream.read(buff)) > 0) {//把從服務(wù)端讀取的文件流保存到ByteArrayOutputSteam中byteArray.write(buff, 0, len);byteArray.flush();}inputStream.close();((CloseableHttpResponse) fileResponse).close();} }

總結(jié)

以上是生活随笔為你收集整理的httpclient 下载文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。