httpclient java 下载_一段用httpClient下载的代码

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.net.URLConnection;

import java.util.concurrent.TimeUnit;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.HttpClientBuilder;

public class DownloadClient {

private String usr = null;

private String pwd = null;

private File localPath = null;

public DownloadClient(File targetPath) {

this.localPath = targetPath;

}

public DownloadClient(File targetPath, String usr, String pwd) {

this.localPath = targetPath;

this.usr = usr;

this.pwd = pwd;

}

public void downLoad(String urlstr, String fileName) {

HttpClient httpClient = HttpClientBuilder.create().build();

OutputStream out = null;

InputStream in = null;

try {

HttpGet httpGet = new HttpGet(urlstr);

httpGet.addHeader("userName", usr);

httpGet.addHeader("passwd", pwd);

httpGet.addHeader("fileName", fileName);

HttpResponse httpResponse = httpClient.execute(httpGet);

HttpEntity entity = httpResponse.getEntity();

in = entity.getContent();

long length = entity.getContentLength();

if (length <= 0) {

System.out.println("下载文件不存在!");

return;

}

System.out.println("The response value of token:" + httpResponse.getFirstHeader("token"));

File file = new File(localPath, fileName);

if(!file.exists()){

file.createNewFile();

}

out = new FileOutputStream(file);

byte[] buffer = new byte[4096];

int readLength = 0;

while ((readLength=in.read(buffer)) > 0) {

byte[] bytes = new byte[readLength];

System.arraycopy(buffer, 0, bytes, 0, readLength);

out.write(bytes);

}

out.flush();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

if(in != null){

in.close();

}

} catch (IOException e) {

e.printStackTrace();

}

try {

if(out != null){

out.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

public void download3(String urlstr, String fileName) {

FileOutputStream out = null;

InputStream in = null;

try{

URL url = new URL(urlstr);

URLConnection urlConnection = url.openConnection();

HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;

// true -- will setting parameters

httpURLConnection.setDoOutput(true);

// true--will allow read in from

httpURLConnection.setDoInput(true);

// will not use caches

httpURLConnection.setUseCaches(false);

// setting serialized

httpURLConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");

// default is GET

httpURLConnection.setRequestMethod("POST");

httpURLConnection.setRequestProperty("connection", "Keep-Alive");

httpURLConnection.setRequestProperty("Charsert", "UTF-8");

// 1 min

httpURLConnection.setConnectTimeout(60000);

// 1 min

httpURLConnection.setReadTimeout(60000);

httpURLConnection.addRequestProperty("userName", usr);

httpURLConnection.addRequestProperty("passwd", pwd);

httpURLConnection.addRequestProperty("fileName", fileName);

// connect to server (tcp)

httpURLConnection.connect();

in = httpURLConnection.getInputStream();// send request to

// server

File file = new File(fileName);

if(!file.exists()){

file.createNewFile();

}

out = new FileOutputStream(file);

byte[] buffer = new byte[4096];

int len = -1;

while ((len = in.read(buffer)) > 0) {

byte[] bytes = new byte[len];

System.arraycopy(buffer, 0, bytes, 0, len);

out.write(bytes);

}

out.flush();

}catch(Exception e){

e.printStackTrace();

}finally{

try {

if(in != null){

in.close();

}

} catch (IOException e) {

e.printStackTrace();

}

try {

if(out != null){

out.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

public static void main(String[] args) {

String usr = "";

String pwd = "";

String url = "http://forspeed.onlinedown.net/down/QQGameMini_1080000016_0.zip";

String fn = "QQGameMini_1080000016_0.zip";

File localPath = new File("E:\\test");

DownloadClient dc = new DownloadClient(localPath);

//Download download = new Download(usr, pwd);

dc.downLoad(url, fn);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值