java 字节流请求 ,文件请求下载

时间:2021-05-23 20:57:13


话不多说,直接上代码

URL urlfile = null;

HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
try {
urlfile = new URL(
"访问链接");
httpUrl = (HttpURLConnection) urlfile.openConnection();
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
int len = 2048;
byte[] b = new byte[len];
while ((len = bis.read(b)) != -1) {
// 循环读取
System.out.println(new String(b, "utf-8"));
}
bis.close();
httpUrl.disconnect();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}