根据url获取文件demo
public String readFileByUrl(String urlStr) {
String json = null;
HttpURLConnection conn;
try {
conn = (HttpURLConnection) new URL(urlStr).openConnection();
conn.setConnectTimeout(3 * 1000);
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
try (InputStream in = conn.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
json = new String(bos.toByteArray());
}
} catch (IOException e) {
e.printStackTrace();
}
return json;
}
本文标题:根据url获取文件demo
本文链接:https://www.haomeiwen.com/subject/swxlsqtx.html
网友评论