国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看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();
 }

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99久久爱 | 日本欧美一区二区 | 亚洲成av人片在线观看香蕉 | 人人九九精 | 精品一区二区av | 久久99精品久久久久 | 国产免费黄色 | 免费一级片在线 | 久久大陆| 久久中国精品 | 一级毛片免费完整视频 | 欧美1级 | 国产精品久久久久久久久免费桃花 | 亚洲喷水| 狠狠综合久久 | 69久久夜色精品国产69乱www | 99精品国产一区二区青青牛奶 | 老司机午夜影院 | 毛片在线观看网站 | 精品视频在线免费观看 | 婷婷免费视频 | av片免费观看 | 午夜在线电影 | 国产精品国产三级国产aⅴ中文 | 色婷婷综合久久久中文字幕 | 久久一区 | 成人亚洲 | 在线精品一区 | chinese国产一区二区 | 亚洲综合中文字幕在线 | 亚洲精品女人久久 | 欧美 日韩 国产 一区 | 精品欧美一区二区三区久久久 | 日韩国产欧美视频 | 国产中文一区 | 成人在线免费看 | 免费在线看a | 久久久99精品免费观看 | 一本大道色卡1卡2卡3 | 成人激情视频 | 亚洲精品国产a久久久久久 中文字幕在线第一页 |