package com.yanjun;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public
class ImageRequest {
/**
* 经过HTTP协议请求网络图片 图片网址:
* http://pic7.nipic.com/20100526/3355499_161646077541_2.jpg
*
* @param args
*/
public
static
void main(String[] args)
throws Exception {
// 从网络上获取图片--URL对象用来封装路径
URL url =
new URL(
"http://pic7.nipic.com/20100526/3355499_161646077541_2.jpg");
// 打开路径连接---获得HttpURLConnection对象
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// 经过HTTP协议请求网络图片---设置请求方式:get/post
httpURLConnection.setRequestMethod("GET");
// 设置链接超时
httpURLConnection.setConnectTimeout(5000);
// 从外界想手机应用内传递数据----经过输入流获取图片数据
InputStream inputStream = httpURLConnection.getInputStream();
// 从输入流中获取图片的二进制数据----readInputStream()
byte[] data = readInputStream(inputStream);
// 将数据保存到应用文件的根目录下,文件名为系统时间
File inageFile = new File(System.currentTimeMillis() + ".jpg");
//经过文件输出流将二进制数据写到文件中去
FileOutputStream fileOutputStream = new FileOutputStream(inageFile);
//经过write()写进文件中去
fileOutputStream.write(data);
fileOutputStream.close();
}
// 读取输入流的方法
public static byte[] readInputStream(InputStream inSream) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 定义一个缓冲区
byte[] buffer = new byte[1024];
int len = 0;
// 不断的从流里读取数据---while循环---nSream.read(buffer)表示从流里读取数据到缓冲区
// 读取到末尾时,返回值是-1;
while ((len = inSream.read(buffer)) != -1) {
// 将缓冲区的数据写到输出流中
byteArrayOutputStream.write(buffer, 0, len);
}
inSream.close();
return byteArrayOutputStream.toByteArray();
}
}
package com.yanjun;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public
class ImageRequest {
/**
* 经过HTTP协议请求获取网页数据:
* http://www.baidu.com/
*
* @param args
*/
public
static
void main(String[] args)
throws Exception {
// 从网络上获取html-URL对象用来封装路径
URL url =
new URL(
"http://www.baidu.com/");
// 打开路径连接---获得HttpURLConnection对象
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// 经过HTTP协议请求网络html---设置请求方式:get/post
httpURLConnection.setRequestMethod("GET");
// 设置链接超时
httpURLConnection.setConnectTimeout(5000);
// 从外界想手机应用内传递数据----经过输入流获取html数据
InputStream inputStream = httpURLConnection.getInputStream();
// 从输入流中获取html的二进制数据----readInputStream()
byte[] data = readInputStream(inputStream);
//将HTML代码的二进制转换成string类型
String html = new String(data);
//将数据打印在控制台上
System.out.println(html);
inputStream.close();
}
// 读取输入流的方法
public static byte[] readInputStream(InputStream inSream) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 定义一个缓冲区
byte[] buffer = new byte[1024];
int len = 0;
// 不断的从流里读取数据---while循环---nSream.read(buffer)表示从流里读取数据到缓冲区
// 读取到末尾时,返回值是-1;
while ((len = inSream.read(buffer)) != -1) {
// 将缓冲区的数据写到输出流中
byteArrayOutputStream.write(buffer, 0, len);
}
inSream.close();
return byteArrayOutputStream.toByteArray(); } }