⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httptotext.java

📁 数据分析java程序
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package dataanalyse.resources;import java.io.*;import org.apache.commons.httpclient.*;import org.apache.commons.httpclient.methods.*;import org.apache.commons.httpclient.params.*;import org.jdom.*;import org.jdom.output.*;/** * * @author syl */public class HttpToText {private static String url = "http://waterdata.usgs.gov/nwis/dv?referred_module=qw&state_cd=md&begin_date=2009-01-01&end_date=2009-03-31&format=rdb";      public static void CreateTextOutput()      {    	// Create an instance of HttpClient.    	HttpClient client = new HttpClient();   		// Create a method instance.   		GetMethod method = new GetMethod(url);   		// Provide custom retry handler is necessary.   		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3,false));        try        {        	// Execute the method.        	int statusCode = client.executeMethod(method);        	if (statusCode != HttpStatus.SC_OK){        		System.err.println("Method failed:" + method.getStatusLine());        	}        	// Read the response body as Stream        	InputStream responseBody = method.getResponseBodyAsStream();            //save web page body source into text file        	BufferedReader BufData = new BufferedReader(new InputStreamReader(responseBody));            String TextFileName = ("HttpClientDemo.txt");            FileWriter FWriter = new FileWriter(TextFileName);            BufferedWriter BWriter = new BufferedWriter(FWriter);            String InputStreamData = null;            while ((InputStreamData = BufData.readLine()) != null)            {              BWriter.write(InputStreamData);              BWriter.newLine();            }              BWriter.close();              responseBody.close();              }//end try              catch(IOException io)              {                   System.out.println(io);              }      }      public static boolean CreateTextOutput(String URL, String strStart, String strEnd)      {        String TextFileName;        String startStr = strStart.replaceAll("-", "");        String endStr = strEnd.replaceAll("-", "");        TextFileName = startStr+"_"+endStr+".txt";    	// Create an instance of HttpClient.    	HttpClient client = new HttpClient();   		// Create a method instance.   		GetMethod method = new GetMethod(URL);   		// Provide custom retry handler is necessary.   		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3,false));        try        {        	// Execute the method.        	int statusCode = client.executeMethod(method);        	if (statusCode != HttpStatus.SC_OK){        		System.err.println("Method failed:" + method.getStatusLine());                return false;        	}        	// Read the response body as Stream        	InputStream responseBody = method.getResponseBodyAsStream();            //save web page body source into text file        	BufferedReader BufData = new BufferedReader(new InputStreamReader(responseBody));                        FileWriter FWriter = new FileWriter(TextFileName);            BufferedWriter BWriter = new BufferedWriter(FWriter);            String InputStreamData = null;            while ((InputStreamData = BufData.readLine()) != null)            {              BWriter.write(InputStreamData);              BWriter.newLine();            }          BWriter.close();          responseBody.close();          }//end try          catch(IOException io)          {               System.out.println(io);               return false;          }        return true;      }  	public static void createXMLOutput() throws Exception {		BufferedReader reader = new BufferedReader(new FileReader("HttpClientDemo.txt"));		Document doc = new Document();		Element root = new Element("tables");		doc.setRootElement(root);		boolean inTable = false;		Element table = null;		for (String input = reader.readLine(); input != null; input = reader.readLine()) {			if (input.startsWith("#") && inTable == false) {				continue;			} else {				if (inTable == false) {					table = new Element("table");					Element meta = new Element("meta");					root.addContent(table);					table.addContent(meta);					String[] columns = input.split("\t");					for (String column : columns) {						meta.addContent(new Element("column").addContent(column));					}					input = reader.readLine();					inTable = true;				} else {					if (input.startsWith("#")) inTable = false;					else {						Element row = new Element("row");						table.addContent(row);						String[] columns = input.split("\t");						for (String column : columns) {							row.addContent(new Element("value").addContent(column));						}					}				}			}		}		XMLOutputter output = new XMLOutputter();		output.output(doc, new FileWriter("output.xml"));		reader.close();	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -