资源简介

Eclipse下完整的java程序,包含HttpClient的全部jar包。通过java类文件,实现通过链接将文件下载本地

资源截图

代码片段和文件信息

package lgy;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.*;

public class DownLoad {

    private DefaultHttpClient httpClient = new DefaultHttpClient();

    public void downLoad(String url String dst) {
        try {
            HttpGet httpGet = new HttpGet(url);
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity entity = httpResponse.getEntity();
            InputStream in = entity.getContent();
            long length=entity.getContentLength();
            if(length<=0){
                System.out.println(“下载文件不存在!“);
                return;
            }
            OutputStream out = new FileOutputStream(new File(dst));
            saveTo(in out);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void saveTo(InputStream in OutputStream out) throws Exception {
        byte[] data = new byte[1024*1024];
        int index =0;
        while ((index=in.read(data) )!= -1) {
            out.write(data0index);
        }
        in.close();
        out.close();
    }

    public static void main(String args[]) {
        DownLoad downLoad = new DownLoad();
        String url = “http://pan.baidu.com/s/1bPPqfc请修改链接“;
        downLoad.downLoad(url “D:/所下载的文件名(文件名要相同)“);
        
    }
}



 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        721  2016-08-17 09:12  DownloadData\.classpath

     文件        388  2016-08-16 18:04  DownloadData\.project

     文件        598  2016-08-16 18:04  DownloadData\.settings\org.eclipse.jdt.core.prefs

     文件       2612  2016-08-17 12:01  DownloadData\bin\lgy\DownLoad.class

     文件     232771  2016-08-17 09:12  DownloadData\commons-codec-1.6.jar

     文件      60686  2016-08-17 09:12  DownloadData\commons-logging-1.1.1.jar

     文件      21886  2016-08-17 09:12  DownloadData\fluent-hc-4.2.1.jar

     文件     427022  2016-08-17 09:12  DownloadData\httpclient-4.2.1.jar

     文件     115096  2016-08-17 09:12  DownloadData\httpclient-cache-4.2.1.jar

     文件     223374  2016-08-17 09:12  DownloadData\httpcore-4.2.1.jar

     文件      26598  2016-08-17 09:12  DownloadData\httpmime-4.2.1.jar

     文件       1613  2016-08-17 12:01  DownloadData\src\lgy\DownLoad.java

     目录          0  2016-08-17 09:12  DownloadData\bin\lgy

     目录          0  2016-08-16 18:06  DownloadData\src\lgy

     目录          0  2016-08-16 18:04  DownloadData\.settings

     目录          0  2016-08-17 09:12  DownloadData\bin

     目录          0  2016-08-16 18:06  DownloadData\src

     目录          0  2016-08-17 09:12  DownloadData

----------- ---------  ---------- -----  ----

              1113365                    18


评论

共有 条评论