HttpClient下载文件

HttpClient下载文件

需要有的Jar;

  • commons-httpclient-3.1.jar

  • commons-logging-1.04.jar

  • commons-codec-1.3.jar
    代码:

    public class HttpClientTest {
    
      private final static String REMOTE_FILE_URL = "http://www.gjt.org/download/time/java/tar/javatar-2.5.tar.gz";
    
      private final static int BUFFER = 1024;
    
      public static void main(String[] args) {
    
         HttpClient client = new HttpClient();
         GetMethod httpGet = new GetMethod(REMOTE_FILE_URL);
          try {
              client.executeMethod(httpGet);
    
              InputStream in = httpGet.getResponseBodyAsStream();
    
              FileOutputStream out = new FileOutputStream(new File("E:\\test_jar\\javatar-2.5.tar.gz"));
    
              byte[] b = new byte[BUFFER];
              int len = 0;
              while((len=in.read(b))!= -1){
                  out.write(b,0,len);
              }
              in.close();
              out.close();
    
          }catch (HttpException e){
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }finally{
              httpGet.releaseConnection();
          }
          System.out.println("download, success!!");
         }
    }

 上一篇
HashMap遍历的两种方式 HashMap遍历的两种方式
HashMap遍历的两种方式// 第一种: 效率高,以后一定要使用此种方式! Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.
2019-08-19
下一篇 
HttpClient的基本用法 HttpClient的基本用法
HttpClient的基本用法虽然在JDK的java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Comm
2019-08-19
  目录