采用linux下java http实现下载功能

[root@luozhonghua javacode]# vi getHttpResource.java
[root@luozhonghua javacode]# cat getHttpResource.java
/**
  http获取文件
  luozhonghua
*/


import java.io.*;
import java.net.*;


public class getHttpResource{


   private boolean getRemoteFile(String strUrl,String fileName) throws IOException {
   URL url=new URL(strUrl);
   HttpURLConnection conn=(HttpURLConnection)url.openConnection();
   DataInputStream input=new DataInputStream(conn.getInputStream());
   DataOutputStream output=new DataOutputStream(new FileOutputStream(fileName));
   byte [] buffer=new byte[1024*8];
   int count=0;
   while((count==input.read(buffer)) > 0){
      output.write(buffer,0,count);
    }
   output.close();
   input.close();
   return true;
  }


  public static void main(String [] args){
     System.out.println(new getHttpResource().getRemoteFile("http://mirrors.163.com/centos/4.0/readme","/usr/local/src/readme"));
  }

}

[root@luozhonghua javacode]# javac getHttpResource.java

[root@luozhonghua javacode]# java getHttpResource
true

posted @ 2014-08-17 22:45  172257861  阅读(132)  评论(0编辑  收藏  举报