激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - FileUtils擴展readURLtoString讀取url內容

FileUtils擴展readURLtoString讀取url內容

2019-11-01 14:05java教程網 JAVA教程

這篇文章主要介紹了FileUtils擴展readURLtoString使用其支持讀取URL內容為String,支持帶POST傳大量參數,大家參考使用吧

代碼如下:


/**
  * 因為FileUtils不支持,所以添加個方法 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);
 }
 /**
  * 因為FileUtils不支持,所以添加個方法
  *
  * <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的內容(method為post,可指定多個參數)
  * @param url
  * @param encoding
  * @param params map的參數(key為參數名,value為參數值)
  * @return String
  * @throws IOException
  */
 public static String readURLToStringByPOST(URL url, String encoding,Map<String, String> params)
 throws IOException {
  HttpURLConnection con = null;
  // 構建請求參數
  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);
   }
  }
  // 嘗試發送請求
  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();
   }
  }
  // 讀取返回內容
  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();
 }

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 一级黄色影片在线观看 | 护士xxxx| 日本中文一级片 | 爱操影院 | 国产亚洲精品久久久久久网站 | 久久毛片| 午夜视频亚洲 | 黄色av网站免费看 | av电影免费在线看 | 毛片在哪看 | 最新中文字幕日本 | 成人久久 | 中国漂亮护士一级a毛片 | 精品国产91久久久久久 | 97青青 | 视频一区二区在线观看 | 亚洲情视频 | xxxx hd videos | 超碰人人做人人爱 | 久久久久久久一区 | 美女视频黄视大全视频免费网址 | av电影网站在线观看 | 蝌蚪久久窝 | 刘亦菲一区二区三区免费看 | 亚洲综合中文 | h视频在线免费观看 | 99精品视频一区二区 | 成人短视频在线观看免费 | 史上最强炼体老祖动漫在线观看 | www.99av| 99久久久免费视频 | 欧美一区二区三区中文字幕 | 久久夜夜视频 | 亚洲欧美一区二区三区在线观看 | 久久精品免费国产 | 国产成人羞羞视频在线 | 久草在线新视觉 | 国产精品久久久久久久娇妻 | 国产成人综合在线视频 | 久久大胆视频 | 毛片一级片 |