java读取远程url图片并获取图片宽高
2016-06-01 16:27
393 查看
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
public class ReturnImgWH {
//读取远程url图片,得到宽高
public int[] returnImgWH(String imgurl) {
boolean b=false;
try {
//实例化url
URL url = new URL(imgurl);
//载入图片到输入流
java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());
//实例化存储字节数组
byte[] bytes = new byte[100];
//设置写入路径以及图片名称
OutputStream bos = new FileOutputStream(new File("C:\\thetempimg.gif"));
int len;
while ((len = bis.read(bytes)) > 0) {
bos.write(bytes, 0, len);
}
bis.close();
bos.flush();
bos.close();
//关闭输出流
b=true;
} catch (Exception e) {
//如果图片未找到
b=false;
}
int[] a = new int[2];
if(b){//图片存在
//得到文件
java.io.File file = new java.io.File("C:\\thetempimg.gif");
BufferedImage bi = null;
boolean imgwrong=false;
try {
//读取图片
bi = javax.imageio.ImageIO.read(file);
try{
//判断文件图片是否能正常显示,有些图片编码不正确
int i = bi.getType();
imgwrong=true;
}catch(Exception e){
imgwrong=false;
}
} catch (IOException ex) {
ex.printStackTrace();
}
if(imgwrong){
a[0] = bi.getWidth(); //获得 宽度
a[1] = bi.getHeight(); //获得 高度
}else{
a=null;
}
//删除文件
file.delete();
}else{//图片不存在
a=null;
}
return a;
}
public static void main(String[] args) {
ReturnImgWH i = new ReturnImgWH();
int[] a=i.returnImgWH("http://www.baidu.com/img/logo-yy.gif");
if(a==null){
System.out.println("图片未找到!");
}else{
System.out.println("宽为" + a[0]);
System.out.println("高为" + a[1]);
}
}
}
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
public class ReturnImgWH {
//读取远程url图片,得到宽高
public int[] returnImgWH(String imgurl) {
boolean b=false;
try {
//实例化url
URL url = new URL(imgurl);
//载入图片到输入流
java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());
//实例化存储字节数组
byte[] bytes = new byte[100];
//设置写入路径以及图片名称
OutputStream bos = new FileOutputStream(new File("C:\\thetempimg.gif"));
int len;
while ((len = bis.read(bytes)) > 0) {
bos.write(bytes, 0, len);
}
bis.close();
bos.flush();
bos.close();
//关闭输出流
b=true;
} catch (Exception e) {
//如果图片未找到
b=false;
}
int[] a = new int[2];
if(b){//图片存在
//得到文件
java.io.File file = new java.io.File("C:\\thetempimg.gif");
BufferedImage bi = null;
boolean imgwrong=false;
try {
//读取图片
bi = javax.imageio.ImageIO.read(file);
try{
//判断文件图片是否能正常显示,有些图片编码不正确
int i = bi.getType();
imgwrong=true;
}catch(Exception e){
imgwrong=false;
}
} catch (IOException ex) {
ex.printStackTrace();
}
if(imgwrong){
a[0] = bi.getWidth(); //获得 宽度
a[1] = bi.getHeight(); //获得 高度
}else{
a=null;
}
//删除文件
file.delete();
}else{//图片不存在
a=null;
}
return a;
}
public static void main(String[] args) {
ReturnImgWH i = new ReturnImgWH();
int[] a=i.returnImgWH("http://www.baidu.com/img/logo-yy.gif");
if(a==null){
System.out.println("图片未找到!");
}else{
System.out.println("宽为" + a[0]);
System.out.println("高为" + a[1]);
}
}
}
相关文章推荐
- 远程控制技术的应用
- VBScript 打造自己的远程CMDShell附使用教程
- 用批处理实现读取文本文件并实现超链接代码的输出[原创]_DOS/BAT_脚本之家
- ext读取两种结构的xml的代码
- Delphi实现读取系统时间与日期完整实例
- Windows系统中使用C#读取文本文件内容的小示例
- C#从文件或标准输入设备读取指定行的方法
- C++实现读取图片长度和宽度
- C++读取INI配置文件类实例详解
- C++实现读取特定路径下文件夹及文件名的方法
- VC6.0实现读取Excel数据的方法
- C#实现把txt文本数据快速读取到excel中
- MySQL数据库远程访问权限如何打开(两种方法)
- VB读取线程、句柄及写入内存的API代码实例
- 远程无法连接SQL2000及MySQL的原因和解决办法
- php文件读取方法实例分析
- C#逐行读取文件的方法
- C# winfrom实现读取修改xml
- C#实现xml文件的读取与写入简单实例
- JS与jQ读取xml文件的方法