FileUtils擴(kuò)展readURLtoString讀取url內(nèi)容
更新時(shí)間:2014年01月17日 09:45:43 作者:
這篇文章主要介紹了FileUtils擴(kuò)展readURLtoString使用其支持讀取URL內(nèi)容為String,支持帶POST傳大量參數(shù),大家參考使用吧
復(fù)制代碼 代碼如下:
/**
* 因?yàn)镕ileUtils不支持,所以添加個(gè)方法 String content =
* FileUtils.readFileToString(FileUtils.toFile(new
* URL("http://www.baidu.com")));
*
* @param source
* @param encoding
* @return
* @throws IOException
*/
public static String readURLToString(URL source) throws IOException {
return readURLToString(source,null);
}
/**
* 因?yàn)镕ileUtils不支持,所以添加個(gè)方法
*
* <pre>
* String content = FileUtils.readFileToString(FileUtils.toFile(new URL(
* "http://www.baidu.com")), "gb2312");
* </pre>
*
* @param source
* @param encoding
* @return
* @throws IOException
*/
public static String readURLToString(URL source, String encoding)
throws IOException {
InputStream input = source.openStream();
try {
return IOUtils.toString(input, encoding);
} finally {
IOUtils.closeQuietly(input);
}
}
/**
* 讀取url的內(nèi)容(method為post,可指定多個(gè)參數(shù))
* @param url
* @param encoding
* @param params map的參數(shù)(key為參數(shù)名,value為參數(shù)值)
* @return String
* @throws IOException
*/
public static String readURLToStringByPOST(URL url, String encoding,Map<String, String> params)
throws IOException {
HttpURLConnection con = null;
// 構(gòu)建請(qǐng)求參數(shù)
StringBuffer sb = new StringBuffer();
if (params != null) {
for (Entry<String, String> e : params.entrySet()) {
sb.append(e.getKey());
sb.append("=");
sb.append(e.getValue());
sb.append("&");
}
if(sb.length()>0){
sb.substring(0, sb.length() - 1);
}
}
// 嘗試發(fā)送請(qǐng)求
try {
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(),encoding);
if (params != null) {
osw.write(sb.toString());
}
osw.flush();
osw.close();
} catch (Exception e) {
LogFactory.getLog(FileUtils.class).error("POST("+url.toString()+")Error("+e.getMessage()+")",e);
} finally {
if (con != null) {
con.disconnect();
}
}
// 讀取返回內(nèi)容
StringBuffer buffer = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(con
.getInputStream(),encoding));
String temp;
while ((temp = br.readLine()) != null) {
buffer.append(temp);
buffer.append("\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}
相關(guān)文章
解決spring AOP中自身方法調(diào)用無(wú)法應(yīng)用代理的問(wèn)題
這篇文章主要介紹了解決spring AOP中自身方法調(diào)用無(wú)法應(yīng)用代理的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Servlet關(guān)于RequestDispatcher的原理詳解
這篇文章主要介紹了Servlet關(guān)于RequestDispatcher的原理詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
java基本教程之join方法詳解 java多線(xiàn)程教程
本文對(duì)java Thread中join()方法進(jìn)行介紹,join()的作用是讓“主線(xiàn)程”等待“子線(xiàn)程”結(jié)束之后才能繼續(xù)運(yùn)行,大家參考使用吧2014-01-01
詳解JAVA中的Collection接口和其主要實(shí)現(xiàn)的類(lèi)
這篇文章主要介紹了JAVA中的Collection接口和其主要實(shí)現(xiàn)的類(lèi),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Java常用的時(shí)間類(lèi)以及其轉(zhuǎn)化方式
這篇文章主要介紹了Java常用的時(shí)間類(lèi)以及其轉(zhuǎn)化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
ElasticSearch查詢(xún)文檔基本操作實(shí)例
這篇文章主要為大家介紹了ElasticSearch查詢(xún)文檔基本操作實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
SpringBoot全局異常處理與定制404頁(yè)面的方法
這篇文章主要介紹了SpringBoot全局異常處理與定制404頁(yè)面的相關(guān)資料,本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2007-09-09

