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)文章
JDBC+GUI實(shí)現(xiàn)簡(jiǎn)單學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了JDBC+GUI實(shí)現(xiàn)簡(jiǎn)單學(xué)生管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
使用Springboot打成jar包thymeleaf的問(wèn)題
這篇文章主要介紹了使用Springboot打成jar包thymeleaf的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java中toString方法的深度解析與應(yīng)用場(chǎng)景詳解
這篇文章主要介紹了Java中的toString方法及其重寫的重要性和注意事項(xiàng),包括信息的完整性、簡(jiǎn)潔性、格式的統(tǒng)一性、避免性能問(wèn)題和遞歸循環(huán)等問(wèn)題,文中將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04
springCloud gateWay 統(tǒng)一鑒權(quán)的實(shí)現(xiàn)代碼
這篇文章主要介紹了springCloud gateWay 統(tǒng)一鑒權(quán)的實(shí)現(xiàn)代碼,統(tǒng)一鑒權(quán)包括鑒權(quán)邏輯和代碼實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
MybatisPlus更新為null的字段及自定義sql注入
mybatis-plus在執(zhí)行更新操作,當(dāng)更新字段為空字符串或者null的則不會(huì)執(zhí)行更新,本文主要介紹了MybatisPlus更新為null的字段及自定義sql注入,感興趣的可以了解一下2024-05-05
Java?SpringBoot集成文件之如何使用POI導(dǎo)出Word文檔
這篇文章主要介紹了Java?SpringBoot集成文件之如何使用POI導(dǎo)出Word文檔,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08

