public static void main(String[] args) { String pathname = "C:\\Users\\ceshi\\Desktop\\test1.txt"; try{ FileReader reader = new FileReader(pathname); BufferedReader br = new BufferedReader(reader); String line; while ((line = br.readLine()) != null) { // 一次读入一行数据 String s = WebResourceReader.doGet(line); System.out.println(s); } }catch (Exception e){ e.printStackTrace(); } }
package com.ceshi.util;import com.alibaba.fastjson.JSONArray;import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;import org.apache.commons.httpclient.Header;import org.apache.commons.httpclient.HttpStatus;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.params.HttpMethodParams;import org.apache.commons.lang.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.http.HttpHeaders;import java.io.*;import java.net.HttpURLConnection;import java.net.URL;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.Map;import java.util.zip.GZIPInputStream;public class WebResourceReader { private static final Logger log = LoggerFactory.getLogger(WebResourceReader.class); protected static final Logger STDERR = LoggerFactory.getLogger("STDERR"); private static String encoding = "gbk"; private static String getHost(String url){ String regexp = "(http://)?([^/]*)(/?.*)"; return url.replaceAll(regexp, "$2"); } private static String getPath(String url){ return url.replaceAll("http://"+getHost(url), ""); } public static String read(String url) { StringBuffer buffer = new StringBuffer(); BufferedReader in = null; GZIPInputStream gzin = null; GetMethod getMethod = null; try { PostUrl pu = new PostUrl(); pu.setHost(getHost(url)); pu.setPath(getPath(url)); pu.setPort(80); // 创建GET方法的实例 getMethod = HttpClientUtil.getInstance().get(pu); // 使用系统提供的默认的恢复策略 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); // 执行getMethod int statusCode = getMethod.getStatusCode(); if (statusCode != HttpStatus.SC_OK) { log.error("读取远程资源文件失败: "+ url); } InputStream is = getMethod.getResponseBodyAsStream(); String contEncoding =""; Header head = getMethod.getResponseHeader( "Content-Encoding"); if(null!=head){ contEncoding = head.getValue(); } /** * 如果文件使用GZIP压缩,则用GZIP流进行处理 */ if (StringUtils.isNotBlank(contEncoding) && contEncoding.contains("gzip")) { // 读取内容 gzin = new GZIPInputStream(is); in = new BufferedReader(new InputStreamReader(gzin, encoding)); } else { in = new BufferedReader(new InputStreamReader(is, encoding)); } String inputLine; while ((inputLine = in.readLine()) != null) { buffer.append(inputLine); } }catch (Exception e) { log.error("##read error=",e); } finally { // 释放连接 if(getMethod!=null){ getMethod.releaseConnection(); } try { if(in!=null) in.close(); } catch (IOException e) { log.error("Read remote file exception : ",e); } } return buffer.toString(); } /** * HttpClientUtil get方法 * @param url * @return * @throws Exception */ public static String getHttpResponse(String url) throws Exception{ BufferedReader in = null; InputStream is = null; GetMethod getMethod = null; try { PostUrl pu = new PostUrl(); pu.setHost(getHost(url)); pu.setPath(getPath(url)); pu.setPort(80); getMethod = HttpClientUtil.getInstance().get(pu); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler()); int statusCode = getMethod.getStatusCode(); log.info("##getHttpResponse-url="+url+"|http-status="+ statusCode); is = getMethod.getResponseBodyAsStream(); StringBuffer buffer = new StringBuffer(); in = new BufferedReader(new InputStreamReader(is, "utf-8")); String inputLine; while ((inputLine = in.readLine()) != null) { buffer.append(inputLine); } return buffer.toString(); } catch (Exception e) { STDERR.error("##getHttpResponse error=",e); throw e; }finally{ try { if(is!=null) is.close(); if(in!=null) in.close(); if(getMethod!=null) getMethod.releaseConnection(); } catch (IOException e) { e.printStackTrace(); } } } /** * HttpURLConnection Post方法 * @param url * @param param * @return * @throws Exception */ public static String doPost(String url, String param) throws Exception { URL url1 = null; BufferedReader reader = null; PrintWriter writer = null; HttpURLConnection conn = null; try { url1 = new URL(url); conn = (HttpURLConnection) url1.openConnection(); conn.setConnectTimeout(20000); conn.setReadTimeout(20000); conn.setRequestMethod("POST"); conn.setInstanceFollowRedirects(true); conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)"); conn.setDoInput(true); conn.setDoOutput(true); writer = new PrintWriter(conn.getOutputStream()); writer.print(param); writer.flush(); int resCode = conn.getResponseCode(); log.info("##doPost……url="+url+",param="+param+", Response code is " + resCode); if(resCode==200) { reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); StringBuffer sb = new StringBuffer(); String line; while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } return sb.toString(); } return ""; } catch (IOException e) { STDERR.error("##HTTP Request is error,url="+url+",param="+param+", error=",e); throw e; } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { STDERR.error("##doPost error1:",e1); } } if (writer != null) { try { writer.close(); } catch (Exception e) { STDERR.error("##doPost error2:",e); } } if (conn != null) try { conn.disconnect(); } catch (Exception e) { STDERR.error("##doPost error3:",e); } } } /** * headers的value暂时支持1个参数 * @param url * @param param * @param headers * @return */ public static String doPost(String url, String param, HttpHeaders headers) throws Exception { Iterator iterator = headers.entrySet().iterator(); HashMap