JAVA用URLダウンロードページは静的ページ


1.プロジェクトの動的ページとして静的ページを生成できる機能
2.JSP泥棒として使える
3.全文網摘とすることができる
package richin.reflect;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class PageStatic {
	public static void convert2Html(String sFilePath,String sSavePath,String sHtmlFile) throws IOException
	{
		PageStatic ru = new PageStatic(); 
		String baseHref="<base href='"+sFilePath+"'></base>";
		String filePath = ru.getClass().getResource("/richin/reflect/PageStatic.class").getPath().toString(); //       
		filePath = filePath.substring(1, filePath.indexOf("WEB-INF")); 
	
		int HttpResult; 
		String SavePath = filePath + sSavePath; //     
		URL url=new URL(sFilePath); 
		URLConnection urlconn=url.openConnection(); //    URLConnection        ,         URL        ,    URL     openConnection          
		urlconn.addRequestProperty("Accept-Language", "zh-cn");
		urlconn.connect(); //   connect                
		HttpURLConnection httpconn=(HttpURLConnection)urlconn; //   HttpURLConnection             ,                 HTTP          

		HttpResult=httpconn.getResponseCode(); //getResponseCode    HTTP           

		if(HttpResult!=HttpURLConnection.HTTP_OK) { 
		} else { 
			String charset=httpconn.getContentType();
			//System.out.println(charset);
			String charType=charset.substring(charset.lastIndexOf("=")+1);
			//System.out.println(charType);
			if("text/html".equals(charType))
				charType="GBK";
		InputStreamReader isr = new InputStreamReader(httpconn.getInputStream(),charType); 
		BufferedReader in = new BufferedReader(isr); 
		String inputLine; 
		if(!SavePath.endsWith("/")) { 
		SavePath+="/"; 
		} 
		FileOutputStream fout = new FileOutputStream(SavePath+sHtmlFile); 
		while ((inputLine = in.readLine()) != null) 
		{ 
		//System.out.println(inputLine);
		if(inputLine.toLowerCase().equals("<head>"))
		inputLine=inputLine+System.getProperty("line.separator")+baseHref;//  basehref
		inputLine=inputLine+System.getProperty("line.separator");  //  
		fout.write(inputLine.getBytes()); 
		} 
		in.close(); 
		fout.close(); 
		} 

	}
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		convert2Html("http://www.51cto.com/art/200807/79080.htm","/","a4.htm"); 
	}

}