以图片地址下载图片

读取给定图片文件的内容,用FileInputStream

	public static byte[] mReaderPicture(String filePath) {
byte[] arr = null;
try {
File file = new File(filePath);
FileInputStream fReader = new FileInputStream(file);
arr = new byte[1024*100];
fReader.read(arr);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return arr;
}

根据byte数组,创建一张新图。

	public static void mWriterPicture(String newFileName,byte[] b){
try {
File file = new File(newFileName);
FileOutputStream fStream = new FileOutputStream(file);
fStream.write(b);
fStream.close();
System.out.println("图片创建成功 "+b.length);
} catch (Exception e) {
// TODO: handle exception
}
}

获取指定网址的图片,返回其byte[]

	public static byte[] mReaderPictureToInternet(String strUr1){
byte[] imgData = null;
URL url;
try {
url = new URL(strUr1);
URLConnection connection = url.openConnection();
int length = (int)connection.getContentLength();
InputStream is = connection.getInputStream();
if (length!=-1) {
imgData = new byte[length];
byte[] temp = new byte[500*1024];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(temp))>0) {
System.arraycopy(temp, 0, imgData, destPos, readLen);
//arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
//从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束
destPos+=readLen;
}
}
return imgData;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return imgData;
}

直接获取指定网址的图片

	public static void DownPictureToInternet(String filePath,String strUr1){
try {
URL url = new URL(strUr1);
InputStream fStream = url.openConnection().getInputStream();
int b = 0;
FileOutputStream fos = new FileOutputStream(new File(filePath));
while ((b=fStream.read())!=-1) {
fos.write(b);
}
fStream.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

应用:

	public static void main(String[] args) {
mWriterPicture("test/1.jpg", mReaderPicture("res/me.jpg"));
mWriterPicture("test/2.jpg", mReaderPictureToInternet(
"http://pic2.desk.chinaz.com/file/201209/7/qinghimingyue4_p.jpg"));
DownPictureToInternet("test/下载.jpg",
"http://img3.100bt.com/upload/ttq/20130205/1360069663700.jpg");
}

源码下载:



Java学习笔记——IO操作之以图片地址下载图片的更多相关文章

  1. Java学习笔记——IO操作之对象序列化及反序列化

    对象序列化的概念 对象序列化使得一个程序可以把一个完整的对象写到一个字节流里面:其逆过程则是从一个字节流里面读出一个事先存储在里面的完整的对象,称为对象的反序列化. 将一个对象保存到永久存储设备上称为 ...

  2. Java学习笔记-IO

    IO(Input Output)流,用来处理设备之间的数据传输 IO IO概述 Java对数据的操作是通过流的方式 Java用于操作流的对象都在IO包中 流按操作数据分为两种:字节流与字符流 流按流向 ...

  3. java学习笔记 --- IO(2)

    IO流的分类:  流向:  输入流  读取数据  输出流 写出数据  数据类型: 字节流 字节输入流  读取数据 InputStream 字节输出流  写出数据 OutputStream 字符流 字符 ...

  4. C#之通过图片地址下载图片

    因为项目上需要加载在线卫星云图,因此写了这个功能来把卫星云图下载的本地,在这里记录一下: string imageUrl=“http://image.nmc.cn/product/2018/08/06 ...

  5. Java 学习笔记 IO流与File操作

    可能你只想简单的使用,暂时不想了解太多的知识,那么请看这里,了解一下如何读文件,写文件 读文件示例代码 File file = new File("D:\\test\\t.txt" ...

  6. java学习笔记--IO流

    第十二章大纲: I/O input/output 输入/输出 一.创建文件,借助File类来实现 file.createNewFile() : 创建文件 file.exists() : 判断文件是否存 ...

  7. java学习笔记IO之字节输入输出流

    IO字节输入输出流 OutputStream:字节输出流 该抽象类是所有字节输出流的超类: 定义了一些共性的成员方法: 1.写入一个字节 void write(int b);//b表示字节 2.写入字 ...

  8. java学习笔记IO之File类

    File类总结 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Times } p.p2 { margin: 0.0px 0.0px 0.0p ...

  9. java学习笔记—第三方操作数据库包专门接收DataSource-dbutils (30)

    Dbutils 操作数据第三方包.依赖数据源DataSource(DBCP|C3p0). QueryRunner – 接收DataSource|Connection,查询数据删除修改操作.返回结果. ...

随机推荐

  1. Web Service(二):cxf 实现

    1. cxf简介 Web Services 的一种实现方式. Apache CXF = Celtix + XFire,后更名为 Apache CXF ,简称为 CXF. CXF 继承了 Celtix ...

  2. bzoj 2753: [SCOI2012]滑雪与时间胶囊 -- 最小生成树

    2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec  Memory Limit: 128 MB Description a180285非常喜欢滑雪.他来到一座雪山,这 ...

  3. java使用代理模拟http get请求

    直接上代码: import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.InetSocketAd ...

  4. JTAG Simplified

    JTAG Simplified So the other day, I explored the JTAG bus interface which is frequently found in CPL ...

  5. STM32输入捕获模式设置并用DMA接收数据

    参考: STM32的PWM输入模式设置并用DMA接收数据 Input capture mode The input stage samples the corresponding TIx input ...

  6. Windows Sysinternals实战指南

    http://www.epubit.com.cn/book/details/4786 Mark Russinovich是Microsoft Azure首席技术官,主要负责微软云计算平台的技术战略和架构 ...

  7. iOS十进制切割格式转换

    //@"123456789" 转换后 @"123,456,789" @interface NSString (num) - (NSString *)money; ...

  8. cmd 递归删除目录或文件

    递归删目录 for /r <TARGET DIR> %i in (<DIR NAME or Pattern>) do rd /s /q %i 递归删文件 for /r < ...

  9. AngularJS中自定义过滤器

    AngularJS中为我们提供了一些内置的过滤器,这里列举一些自定义过滤器的场景. 自定义过滤器,不带参赛 //过滤 不带参赛 app.filter('ordinal', function () { ...

  10. WordPress基础:设置后台语言

    菜单-设置-站点语言