javaswing吧 关注:107贴子:327
  • 1回复贴,共1

一段用httpClient下载的代码

只看楼主收藏回复

网上找的,改了改。百度搜“httpclient 下载”就能找到。
httpClient是apache的开源项目,3.0之后改名为“HttpComponents”。搜“httpClient”时,会搜到新旧两个版本。
新版标题是:
Apache HttpComponents – HttpComponents HttpClient Overview
创建Maven项目,在pom.xml里加上:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
2楼代码。


IP属地:山东1楼2020-05-15 10:54回复
    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);
    }
    }


    IP属地:山东2楼2020-05-15 11:02
    回复