http协议GET方式获取图片
来源:互联网 发布:linux 查找匹配字符串 编辑:程序博客网 时间:2024/06/05 19:27
HttpUtils.java
package com.http.get;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class HttpUtils {// 自定义的web服务器的资源private static String URL_PATH = "http://172.16.47.156:8080/myhttp/2.jpg";public HttpUtils() {// TODO Auto-generated constructor stub}public static void saveImageToDisk() throws IOException {InputStream inputStream = getInputStream();byte[] data = new byte[1024];int len = 0;FileOutputStream fileOutputStream = null;try {fileOutputStream = new FileOutputStream("D:\\Temp\\testhttp.jpg");while ((len = inputStream.read(data)) != -1) {fileOutputStream.write(data, 0, len);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (inputStream != null) {try {inputStream.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (fileOutputStream != null) {try {fileOutputStream.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}/** * 获得服务器端数据,以InputStream形式返回 * * @return * @throws IOException */public static InputStream getInputStream() throws IOException {InputStream inputStream = null;HttpURLConnection httpURLConnection = null;try {URL url = new URL(URL_PATH);if (url != null) {httpURLConnection = (HttpURLConnection) url.openConnection();// 设置连接网络的超时时间httpURLConnection.setConnectTimeout(3000);httpURLConnection.setDoInput(true);// 设置本次http请求使用get方式请求httpURLConnection.setRequestMethod("GET");int responseCode = httpURLConnection.getResponseCode();if (responseCode == 200) {// 从服务器获得一个输入流inputStream = httpURLConnection.getInputStream();}}} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return inputStream;}public static void main(String[] args) throws IOException {// 从服务器获得图片保存到本地saveImageToDisk();System.out.println("传输步骤完毕");}}
自定义服务器web项目
- http协议GET方式获取图片
- Http协议获取图片数据流(GET方式)
- http编程系列2:http协议GET方式获取网络图片
- http协议get方式从服务器获取图片并保存到本地
- http协议get方法获取图片并保存到本地
- http协议3---GET方式和POST方式获取表单数据举例2(统一方式获取)
- Netty_使用http协议,post&get方式
- Http协议 GET/POST请求方式
- 通过Http协议以Get和Post方式获取服务器端文本数据
- JAVA用http协议GET方法从服务器获取图片保存到本地
- HTTP协议传输中GET方式与POST方式区别
- http协议2---GET方式和POST方式获取表单数据举例 1(非统一方式获取数据,比较麻烦)
- http协议4---GET方式和POST方式获取表单数据举例3(统一方式获取,解决中文乱码问题)
- 使用http协议获取网络图片
- 使用http协议获取网络图片
- 使用http协议获取网络图片
- http协议的get和post方式的区别
- HTTP协议请求方式:GET 和 POST区别
- Every derived table must have its own alias ——异常
- hashCode
- 编译期确定类型关系
- 环境变量的配置导致无法登录
- SQL Server安装程序遇到以下错误:MsiGetProductInfo无法检索 Product Code 为“{}”的包的ProductVersion.错误代码:1
- http协议GET方式获取图片
- 兼容ie8的tab选项卡
- 无聊耍博客~~
- HTML5的沿革提供了一些新的元素和属性
- 如何用递归树求快速排序时间复杂度
- Codeforces Round #127 (Div. 1)
- SEO基本概念之死链接
- 算法之二分搜索法
- MDCC为移动开发者服务:一看、一聊、一聚