HTPPを通じてテキストコンテンツ(Java)のルートを取得します。

readFileByUrl公共の静的な文字列(文字列urlStr){ 
文字列RES = NULL;
試み{
URL =新しい新しいURL URL(urlStr);
HttpURLConnectionのコネティカット=(HttpURLConnectionの)url.openConnection();
//時間をかけて設定は3秒です
conn.setConnectTimeout(3 * 1000);
//シールド403のリターンをクロールすることを防止エラー
conn.setRequestProperty( "User--エージェント"、 "Mozillaは/ 4.0(互換; MSIE 5.0; Windows NTの; DigExt)");
//取得した入力ストリーム
InputStream InputStreamはconn.getInputStream =();
RES = readInputStream(InputStreamの);
}キャッチ(例外E){
System.out.printlnは( "テキストアドレスはurlで失敗GET");
}
戻りRES;
}


/ **
*入力ストリームから内の文字列を取得します。
*
* @paramのInputStreamの
* @return
* @throwsのIOException
* /
パブリック静的文字列readInputStream(InputStream InputStreamは)のIOException {スロー
バイト[]バッファ=新しいバイト[1024];
int型のlen = 0;
ByteArrayOutputStream BOS =新しいByteArrayOutputStream();
(!(LEN = inputStream.read(バッファ))= -1){一方
bos.write(バッファ、0、LEN)。
}
bos.close()。
新しいStringを返す(bos.toByteArray()、 "UTF-8");
}

おすすめ

転載: www.cnblogs.com/NowShowTimeChenKang/p/11358665.html