详解GET方法:HttpURLConnection从网络获取资源
来源:互联网 发布:云计算优缺点 编辑:程序博客网 时间:2024/04/30 18:47
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author Administrator
*
*/
public class Demo01_HttpURLConnection {
public static void main(String[] args) {
FileOutputStream fos = null;
// step1:先获取网络地址
String baseUrl = "https://www.baidu.com/img/bd_logo1.png";
// step2:根据网址,构建URL对象。用于打开和该网址指向的资源,建立连接
try {
URL url = new URL(baseUrl);
// step3:根据url,打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// step4:设置本次网络请求的参数
// 设置请求方式
connection.setRequestMethod("GET");// 该方法的参数:所有的参数字母全部大写。
// 设置连接超时
connection.setConnectTimeout(5000);
// step5:开始连接
connection.connect();// 该行代码可以省略
// step6:获取服务端的响应码
int responseCode = connection.getResponseCode();
System.out.println(responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {// 200
// step7:获取流,读数据
InputStream inputStream = connection.getInputStream();
String fileName = baseUrl.substring(baseUrl.lastIndexOf("/") + 1);
File file = new File("D:\\Ruby\\pro", fileName);
fos = new FileOutputStream(file);
byte[] bs = new byte[1024];
int len = 0;
while ((len = inputStream.read(bs)) != -1) {
fos.write(bs, 0, len);
}
System.out.println("图片下载完毕。。。");
}
// connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* 客户端程序:用于根据指定的网址。访问网络的数据
// 拿获取百度服务器端的logo图片为例
* @author Administrator
*
*/
public class Demo01_HttpURLConnection {
public static void main(String[] args) {
FileOutputStream fos = null;
// step1:先获取网络地址
String baseUrl = "https://www.baidu.com/img/bd_logo1.png";
// step2:根据网址,构建URL对象。用于打开和该网址指向的资源,建立连接
try {
URL url = new URL(baseUrl);
// step3:根据url,打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// step4:设置本次网络请求的参数
// 设置请求方式
connection.setRequestMethod("GET");// 该方法的参数:所有的参数字母全部大写。
// 设置连接超时
connection.setConnectTimeout(5000);
// step5:开始连接
connection.connect();// 该行代码可以省略
// step6:获取服务端的响应码
int responseCode = connection.getResponseCode();
System.out.println(responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {// 200
// step7:获取流,读数据
InputStream inputStream = connection.getInputStream();
String fileName = baseUrl.substring(baseUrl.lastIndexOf("/") + 1);
File file = new File("D:\\Ruby\\pro", fileName);
fos = new FileOutputStream(file);
byte[] bs = new byte[1024];
int len = 0;
while ((len = inputStream.read(bs)) != -1) {
fos.write(bs, 0, len);
}
System.out.println("图片下载完毕。。。");
}
// connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
0 0
- 详解GET方法:HttpURLConnection从网络获取资源
- HttpURLConnection网络获取资源之:POST方法
- HttpURL网络获取资源之:GET方法
- 使用HttpURLconnection从网络获取图片
- get获取网络方法
- HttpURLConnection-Get方法实现
- HttpURLConnection获取网络图片
- HttpURLConnection网络获取
- HttpUrlConnection Get请求网络数据
- HttpURLConnection,get,post,网络请求
- HttpURLConnection 网络请求 get post
- Android网络请求详解,从HTTP协议到httpUrlConnection
- android HttpURLConnection编程详解,获取网络图片,html代码查看
- android HttpURLConnection编程详解,获取网络图片,html代码查
- Unity_ngui_从网络获取图片资源
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- 从网络获取图片方法
- 第十二篇:Android GET请求获取网络数据以及POST数据(使用HttpURLConnection)
- Java中有关Null的9件事
- Android简易手势密码开源库
- c++加载python模块,但是PyImport_ImportModule老返回NULL
- 讲解JDBC怎么演变到Mybatis的渐变过程
- 【c++ primer 笔记】第九章 顺序容器
- 详解GET方法:HttpURLConnection从网络获取资源
- mysql能够做的优化内容
- QQ侧滑菜单效果
- 89. Gray Code
- transient关键字
- web 框架集合
- Spring-Boot1.4.0项目部署问题小记
- 逻辑回归(Logistic Regression)和SVM的联系以及Kernel
- 大杂烩