Java学习笔记——IO操作之以图片地址下载图片
以图片地址下载图片
读取给定图片文件的内容,用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操作之以图片地址下载图片的更多相关文章
- Java学习笔记——IO操作之对象序列化及反序列化
对象序列化的概念 对象序列化使得一个程序可以把一个完整的对象写到一个字节流里面:其逆过程则是从一个字节流里面读出一个事先存储在里面的完整的对象,称为对象的反序列化. 将一个对象保存到永久存储设备上称为 ...
- Java学习笔记-IO
IO(Input Output)流,用来处理设备之间的数据传输 IO IO概述 Java对数据的操作是通过流的方式 Java用于操作流的对象都在IO包中 流按操作数据分为两种:字节流与字符流 流按流向 ...
- java学习笔记 --- IO(2)
IO流的分类: 流向: 输入流 读取数据 输出流 写出数据 数据类型: 字节流 字节输入流 读取数据 InputStream 字节输出流 写出数据 OutputStream 字符流 字符 ...
- C#之通过图片地址下载图片
因为项目上需要加载在线卫星云图,因此写了这个功能来把卫星云图下载的本地,在这里记录一下: string imageUrl=“http://image.nmc.cn/product/2018/08/06 ...
- Java 学习笔记 IO流与File操作
可能你只想简单的使用,暂时不想了解太多的知识,那么请看这里,了解一下如何读文件,写文件 读文件示例代码 File file = new File("D:\\test\\t.txt" ...
- java学习笔记--IO流
第十二章大纲: I/O input/output 输入/输出 一.创建文件,借助File类来实现 file.createNewFile() : 创建文件 file.exists() : 判断文件是否存 ...
- java学习笔记IO之字节输入输出流
IO字节输入输出流 OutputStream:字节输出流 该抽象类是所有字节输出流的超类: 定义了一些共性的成员方法: 1.写入一个字节 void write(int b);//b表示字节 2.写入字 ...
- 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 ...
- java学习笔记—第三方操作数据库包专门接收DataSource-dbutils (30)
Dbutils 操作数据第三方包.依赖数据源DataSource(DBCP|C3p0). QueryRunner – 接收DataSource|Connection,查询数据删除修改操作.返回结果. ...
随机推荐
- 005.iSCSI客户端配置示例-Windows
一 环境 Linux作为iSCSI服务端,Windows2008R2作为iSCSI客户端 二 服务端配置过程 2.1 客户端配置 在Linux上参照之前的配置建立三个LUN卷作为共享盘,最终配置如下: ...
- android 多进程
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha android应用中 可以通过 清单文件的属性配置,创建出一个新进程. 多进程的好处,当 ...
- 缩减apk大小
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 1,重复的资源,不用的资源,删去. 2,使用混淆,可以优化. 3,尽量的使用代码,或者其 ...
- 关于操作Access数据库jdk选择问题
关于操作Access数据库,使用jdk64位无法通过ODBC无法获取数据,只能通过jdk32位进行开发.
- Codeforces Round #258 (Div. 2) D. Count Good Substrings 水题
D. Count Good Substrings 题目连接: http://codeforces.com/contest/451/problem/D Description We call a str ...
- consul vs etcd3
https://sysadmin.libhunt.com/project/etcd/vs/consul
- JTAG – A technical overview and Timing
This document provides you with interesting background information about the technology that underpi ...
- swap文件查看
建议 Swap 使用单独的分区: a swap file a combination of swap partitions and swap files. Swap 大小的计算公式: M 等于物理内存 ...
- bat如何批量删除指定部分文件夹名的文件夹
@echo offfor /f "delims=" %%i in ('dir /s/b/ad 123*') do ( rd /s/q "%%~i")exit
- [Winfrom]Cefsharp配置与初始化
摘要 在做客户端程序的时候,本来打算使用wpf的,但在内嵌cefsharp的时候,发现输入法有问题,所以使用了winform作为cefsharp的容器. 系列文章 CefSharp 在同一窗口打开链接 ...