FileUtils拡張readURLtoStringはurlの内容を読みます。

2899 ワード

 
  
/**
  * 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 ,
  *
  *
 
  
  * String content = FileUtils.readFileToString(FileUtils.toFile(new URL(
  *   "http://www.baidu.com")), "gb2312");
  *
  *
  * @param source
  * @param encoding
  * @return
  * @throws IOException
  */
 public static String readURLT String(URL source,String encoding)
   throws IOException{
  InputStream input=source.openStream()
  try{
   return IOUtils.toString;
  } finally{
   IOUtils.closeQuietly;
  }
 }
 /**
  * urlの を み る(methodはpostで、 のパラメータを することができます)
  * @パラムurl
  * @param encoding
  * @param params mapのパラメータ(keyはパラメータ 、valueはパラメータ )
  * @return String
  * @throws IOException
  */
 public static String readURLT StringBy POST(URL url、String encoding、Map params)
 throws IOException{
  HttpURLConnection con=null;
  // パラメータの
  StringBuffer sb=new StringBuffer();
  if(params!=null){
   for(Entry e:params.entrySet(){
    sb.apped(e.getKey();
    sb.apped(「=」)
    sb.apped(e.getValue();
    sb.apped("&")
   }
   if(sb.length()>0){
    sb.substring(0,sb.length()-1)
   }
  }
  // を しようとします
  try{
   コンサート=(HttpURLConnection)url.openConnection()
   con.set Request Method(「POST」);
   con.set Douutput(true);
   con.set DoInput(true);
   con.setUseCaches(false);
   con.set Request Property(「Content-Type」、「appication/x-wn-form-urlencoded」);
   OutputStream Writerosw=new OutputStream Writer;
   if(params!=null){
    osw.write(sb.toString();
   }
   osw.flash()
   osw.close()
  } catch(Exception e){
   Log Factory.get Log(FileUtils.class).error("POST("+url.toString()+")Error("+"+e.get Message()+""",e);
  } finally{
   if(con!=null){
    con.disconnect()
   }
  }
  // り を みだします
  StringBurer buffer=new StringBurer()
  try{
   BufferedReader br=new Bufferedier(new Input Stream Reader)
     .get InputStream(),encoding);
   String temp;
   while((temp=b.readline)!=null){
    buffer.apped(temp);
    buffer.apped(");
   }
  } catch(Exception e){
   e.print StockTrace();
  }
  return buffer.toString() }