java url 读取JS文件 中文乱码

package mode;

import java.io.*;
import java.net.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class Test {

	public static void GetFromHtml() throws IOException{   
//		int ret=0;   
		String contentEncoding;   
		HttpURLConnection url=null;   
		//String htmladdr="http://www.zhcw.com/data-js/nowdata50.js";   
		String htmladdr="http://company.zhaopin.com/P9/CC1433/7728/CC143377284_Name_1.js";   
		       StringBuffer buffer=new StringBuffer("");   
		       try {   
		        URL url1 = new URL(htmladdr);   
		           
		           
		        url =   (HttpURLConnection)url1.openConnection ();   
		        url.setRequestProperty("User-Agent", "mozlla/5.0");   
		        url.setRequestProperty("Accept-Encoding", "gzip, deflate");   
		        url.connect();   
		           
		           
		        contentEncoding=url.getContentEncoding();   
		        System.out.println(contentEncoding);
		           
		} catch (Exception e) {   
//		 TODO Auto-generated catch block   
		  
		e.printStackTrace();   
		return ;   
		}    
		if(url!=null){   
		InputStream stream;   
//		if ((null != contentEncoding)&& (-1 != contentEncoding.indexOf ("gzip"))){   
//		                   stream = new GZIPInputStream (   
//		                     url.getInputStream ());   
//		            }   
//		            else if ((null != contentEncoding)   
//		                   && (-1 != contentEncoding.indexOf ("deflate")) )  
//		            {   
//		                   stream = new InflaterInputStream (   
//		                      url.getInputStream ());   
//		            }   
//		            else  
//		            {   
		                   stream = url.getInputStream();   
//		            }   
		  
//		InputStream stream= url.getInputStream();   
		      BufferedReader reader =   
		         new BufferedReader(new InputStreamReader(stream));   
		         
		      String temp;   
		          while (null != (temp = reader.readLine())) {   
		          // temp=new String(temp.getBytes("iso-8859-1"),"gb2312");    
		           System.out.println(temp);    
		            // break;    
		          }   
		          reader.close();   
		          }   
		}  
	public void get(){
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test test = new Test();
		try {
			test.GetFromHtml();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

 

你可能感兴趣的:(java,apache,.net)