Die Codekopie lautet wie folgt:
/**
* Weil FileUtils es nicht unterstützt, fügen Sie einen Methode -String -Inhalt hinzu = fügen Sie es hinzu.
* FileUtils.ReadFileToString (FileUtils.tofile (neu
* URL ("http://www.baidu.com")));
*
* @param Quelle
* @param codierung
* @zurückkehren
* @throws ioException
*/
public static String readUrltoString (URL -Quelle) löst ioException {aus
return ReadurltoString (Quelle, NULL);
}
/**
* Da Dateiutils es nicht unterstützt, fügen Sie eine Methode hinzu
*
* <pre>
* String content = FileUtils.readFileToString (FileUtils.toFile (neue URL (neue URL ()
* http://www.baidu.com)), GB2312);
* </pre>
*
* @param Quelle
* @param codierung
* @zurückkehren
* @throws ioException
*/
öffentliche statische String -ReadurltoString (URL -Quelle, String -Codierung)
löst ioException {aus
InputStream input = source.openstream ();
versuchen {
return ioutils.tostring (Eingabe, Codierung);
} Endlich {
Ioutils.closequiet (Input);
}
}
/**
* Lesen Sie den Inhalt der URL (Die Methode ist nach dem Postwerk, mehrere Parameter können angegeben werden).
* @param url
* @param codierung
* @Param Params Map Parameter (Schlüssel ist Parametername, Wert ist Parameterwert)
* @return String
* @throws ioException
*/
public static String ReadurltoStringByPost (URL -URL, String -Codierung, Map <String> Params)
löst ioException {aus
HttpurlConnection con = null;
// Anforderungsparameter erstellen
StringBuffer sb = new StringBuffer ();
if (params! = null) {
für (Eintrag <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);
}
}
// Versuchen Sie, eine Anfrage zu senden
versuchen {
con = (httpurlConnection) url.openconnection ();
con.setRequestMethod ("Post");
con.setDooutput (true);
con.setDoInput (true);
con.setusecaches (falsch);
con.setRequestProperty ("Content-Typ", "Anwendung/x-www-form-urlencoded");
OutputStreamwriter osw = neuer outputStreamWriter (con.getOutputStream (), Codierung);
if (params! = null) {
osw.write (sb.tostring ());
}
osw.flush ();
osw.close ();
} catch (Ausnahme e) {
LogFactory.getLog (FileUtils.class) .Error ("post ("+url.toString ()+") Fehler ("+e.getMessage ()+")", e);
} Endlich {
if (con! = null) {
con.disconnect ();
}
}
// Lesen Sie den Rückgabeinhalt
StringBuffer buffer = new StringBuffer ();
versuchen {
BufferedReader BR = neuer BufferedReader (neuer InputStreamReader (con
.GetInputStream (), Codierung));
String -Temperatur;
while ((temp = br.readline ())! = null) {
buffer.Append (temp);
Buffer.Append ("/n");
}
} catch (Ausnahme e) {
E. printstacktrace ();
}
return buffer.toString ();
}