jdk6.0从入门到精通-----chapter6--http



用httpconnection进行文件下载,建立一般的java项目ch06

package HTTP.HttpConnection;

import java.net.URL;
import java.net.HttpURLConnection;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
//下载程序
public class TestHttpConnection
{    
    public static void main(String[] args) //args[0]为待下载文件的url
    {    	      
    	  try
    	  {
//    		HttpURLConnection对象发出http请求报文,接收响应报文
            HttpURLConnection httpConnection = (HttpURLConnection) new URL(args[0]).openConnection();
            httpConnection.setRequestMethod("GET");		
            BufferedInputStream bis=new BufferedInputStream(httpConnection.getInputStream());
            ByteArrayOutputStream baos=new ByteArrayOutputStream();
            byte[] buff=new byte[1024];
            int bytesRead;
            while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) 
            {
                baos.write(buff, 0, bytesRead);
            }
            buff=null;
            String fileName=args[0].substring(args[0].lastIndexOf("/")+1);
            FileOutputStream fos=new FileOutputStream(fileName); //在当前项目ch06生成一个文件
            fos.write(baos.toByteArray()); //baos.toByteArray()形成字节数组,一次性将字节数组写入文件,但如果远程文件尺寸较大,就会导致JVM内存溢出
            baos.close();
            fos.close();
        }
        catch(IOException ioe)
        {
        	  ioe.printStackTrace();
        }    	
    }   
}

执行时如果传入的参数为http://www.sina.com/index.html,就会在ch06目录下生成一个index.html与sina网的首页内容一样


登录需要验证的web站点,Authenticator会提供账户?

package HTTP.Authenticator;

import java.io.PrintWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.StringWriter;
import java.io.IOException;
import java.net.URL;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.MalformedURLException;

public class TestAuthenticator
{	  
    public static void main(String[] args)
	  {
	  	  Authenticator.setDefault(new MyAuthenticator(args[1],args[2]));//user,password
	      System.out.println( new TestAuthenticator().fetchURL(args[0]) );   //url
    }

    private String fetchURL (String urlString)
	  {
	    StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try 
		    {
		    URL url = new URL(urlString);
            InputStream content = (InputStream)url.getContent();//get url content
            BufferedReader in = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = in.readLine()) != null) //read
		        {
		            pw.println (line); //print in console
            }
        } 
        catch(MalformedURLException e) 
		    {
		        pw.println ("URL无效");
        } 
	      catch(IOException e) 
		    {  
		        pw.println ("读取URL资源时出错");
        }
        return sw.toString();
   }
}

class MyAuthenticator extends Authenticator 
{
	  private String user=null;
	  private String password=null;
	
	  public MyAuthenticator(String user, String password)
	  {
	  	  this.user=user;
	  	  this.password=password;
	  }
	
    protected PasswordAuthentication getPasswordAuthentication() 
    {
	      char[] ch=new char[this.password.length()];
	      password.getChars(0,this.password.length(),ch,0);
	      return new PasswordAuthentication (this.user, ch);
    }
}

你可能感兴趣的:(java,jvm,html,.net,Web)