三种读取网页上的图片方法,以及read() read(byte b)的使用

有人问这个问题,说实话我也就是从本地读读(难道和从网页尚读不一样吗?好像区别不大),就写了几个,中间也发现了不少问题。看代码把


package com;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class TestInternet {
Image image = null;
public static void main(String args[]){
TestInternet ti = new TestInternet();
try {
ti.getResource();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getResource() throws IOException {
String urlStr = "http://i3.itc.cn/20101008/394_06b5e5c1_d16a_48d2_9340_86b0c9292732_1.jpg";
URL url = new URL(urlStr);
image = ImageIO.read(url);
int width = image.getWidth(null);
int height = image.getHeight(null);
BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
bi.getGraphics().drawImage(image, 0, 0, width , height, null);
FileOutputStream fos = new FileOutputStream(new File("myPic.jpg"));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(bi);
fos.flush();
fos.close();

}

}




刚才又写了两种形式 上代码

package com;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;


public class DynamicGetURLImage {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//图片的http全路径
String imgurl = "http://i3.itc.cn/20101008/394_06b5e5c1_d16a_48d2_9340_86b0c9292732_1.jpg";
URL url = new URL(imgurl);
BufferedInputStream in = new BufferedInputStream(url.openStream());
//保存的图片文件名
File img = new File("img.jpg");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(img));
byte[] buf = new byte[2048];
int l = 0;
int temp = 0;
while( (l = in.read(buf)) != -1){
temp =0;
temp += l;
out.write(buf);

}
System.out.println(temp);
in.close();
out.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}





package com.yuanlai;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class TestInternet {
public static void main(String args[]){
TestInternet ti = new TestInternet();
try {
ti.getResource();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getResource() throws IOException {
String urlStr = "http://i3.itc.cn/20101008/394_06b5e5c1_d16a_48d2_9340_86b0c9292732_1.jpg";
int ResponseCode = 0;
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(2 * 1000);
ResponseCode = connection.getResponseCode();
System.out.println(ResponseCode);
if (ResponseCode == 200) {
InputStream inputStream = connection.getInputStream();
byte[] b = readInputStream(inputStream);
inputStream.close();
System.out.println(b.length);
FileOutputStream fos = new FileOutputStream(new File("myPic.jpg"));
fos.write(b);
fos.flush();
fos.close();
}
}

private byte[] readInputStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int l = -1;
int temp = 0;
byte [] by = new byte[1024];
//int temp1 = inputStream.read(by);
/**
while (temp1 != -1) {
//baos.write(l);
baos.write(by, 0, temp1);
temp += l;
temp1 = inputStream.read(by);
}
*/
//read(byte[])是从输入流中读取一定数量的字节并将其存储在缓冲区数组 b 中
//而read() 是读取下一个字节,
while((l = inputStream.read(by)) >= 0){
baos.write(by, 0, l);
}
return baos.toByteArray();
}
}


看来得写个总结了,
第一种方式可以对图片进行操作,比如放大缩小什么的。
第二种比较简洁,适合做简单的应用的时候用,
第三种有点罗嗦,但你可以操作的东西比较多,比如connection对象等。


还有值得总结的地方就是关于read() 和read(byte[] b)
read() 指的是从输入流读取下一个数据字节。返回 0 到 255 范围内的 int 字节值。如果因已到达流末尾而没有可用的字节,则返回值 -1。
read(byte[] b) 是开辟一块缓存区.“从输入流中读取一定数量的字节并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数”
read(b) 方法的效果等同于: read(b, 0, b.length)

注意判断的时候 要while((l = inputStream.read(by)) >= 0) 不能while((l = inputStream.read()) >= 0)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值