Http Get 从服务器中获取数据 存储到本地
来源:互联网 发布:百度地图js api 标注 编辑:程序博客网 时间:2024/05/16 10:10
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;import org.apache.http.message.BasicNameValuePair;public class HttpUtils { private static String URL_PATH = "http://192.168.0.102:8080/myhttp/pro1.png"; public HttpUtils() { // TODO Auto-generated constructor stub } public static void saveImageToDisk() { InputStream inputStream = getInputStream(); byte[] data = new byte[1024]; int len = 0; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream("C:\\test.png"); while ((len = inputStream.read(data)) != -1) { fileOutputStream.write(data, 0, len); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * 获得服务器端的数据,以InputStream形式返回 * @return */ public static InputStream getInputStream() { 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 block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return inputStream; } public static void main(String[] args) { // 从服务器获得图片保存到本地 saveImageToDisk(); }}
0 0
- Http Get 从服务器中获取数据 存储到本地
- JAVA用http协议GET方法从服务器获取图片保存到本地
- http协议get方式从服务器获取图片并保存到本地
- SQL Server 2005用存储过程从服务器中导出数据到本地自己的数据库
- python 从远程服务器中获取文件到本地
- GET方式从服务器获取数据
- http协议get方法获取图片并保存到本地
- 在http请求中,get不都是从服务器上down数据下来吗?
- ExtJS中从WebService获取数据保存到本地,填充GridPanel实现静态数据分页
- http get请求获取服务器返回的应答数据
- http的get方式连接服务器,获取数据
- 从服务器将ORACLE数据导入到本地的方法
- 使用poi从服务器保存数据到本地
- oracle从服务器导出数据到本地的方法
- mysql从本地导到服务器上数据不全
- C#中从服务器中下载execl文件到本地
- 客户端http从服务器获取图片保存到指定位置
- 拍照(从本地获取存储在SD卡中)
- shell中的括号(小括号,中括号,大括号)
- shell 函数 返回数组
- sed
- shell 正则表达式
- HTTP 中 Get 和Post
- Http Get 从服务器中获取数据 存储到本地
- Android中SdCard的数据读写
- Json 从服务器 构建
- Http 通过Get获取数据
- Http 以post方式获取数据
- Android 使用Sharedpreference共享参数
- Android中使用Sqlite数据库 (一) 建表
- Android中使用Sqlite数据库 (二)使用sql 增删改查
- Android中使用Sqlite数据库(三) 使用 SQLiteDatabase类实现 增删改查