?? httptotext.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 + -