一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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();
 }

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品久久久久免费极品大片 | 98精品视频| 亚洲aⅴ男人的天堂在线观看 | 99性视频| 国产成人高清视频 | 精品国产剧情在线观看 | 红杏劫| 含羞草国产亚洲精品岁国产精品 | 亚洲精品丝袜在线一区波多野结衣 | 国产精品高清一区二区三区 | haodiaocao几万部精彩视频 | 国产精品怡红院在线观看 | 成人精品第一区二区三区 | 美女扒开胸罩露出奶 | bt天堂在线观看国产 | 都市风流贵妇激情 | 免费国产白棉袜踩踏区域 | poronovideos变态极限| 久草在线精彩免费视频 | 麻豆找网服 | 国产精品每日在线观看男人的天堂 | 久久精品观看影院2828 | 日韩手机在线观看 | 肉文小说 | 国产成人精品免费久久久久 | 咪咪爱在线视频 | jk制服白丝超短裙流白浆 | 久久www免费人成_看片高清 | 涩情主播在线翻车 | 欧美第一视频 | 亚洲人成在线观看一区二区 | 久久久无码精品无码国产人妻丝瓜 | 亚洲图片综合区 | 欧美日韩中文字幕久久伊人 | 韩剧网3600热播剧 | 经典WC女厕所里TV | 青青久在线视频免费观看 | 成人aaaa| 刺客女仆 | 高h全肉np触手 | 91短视频社区在线观看 |