java处理图片的所有类_JAVA处理图片常用公共类
//检测图片宽高
public static Map getPicWH(String imagePath) {
Map mp = new HashMap();
File _file = new File(imagePath);
Image src;
try {
src = ImageIO.read(_file);
// 图片的宽度
int width = src.getWidth(null); //读取文件的长宽
// 图片的高度
int height = src.getHeight(null);
mp.put("width", width);
mp.put("height", height);
} catch (IOException e) {
e.printStackTrace();
}
return mp;
}
//检测图片大小
public Long image2byte(String path) {
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
} catch (FileNotFoundException ex1) {
ex1.printStackTrace();
} catch (IOException ex1) {
ex1.printStackTrace();
}
return Long.valueOf(data.length);
}
//获取BLOB文件生成图片
public static boolean getBlobFile(InputStream in,String sFullName) {
File file = new File(sFullName);
RandomAccessFile rFile = null;
try {
rFile = new RandomAccessFile(file, "rw");
rFile.seek(0);
byte bytes[] = new byte[1024];
int ch = 0;
while ((ch = in.read(bytes)) != -1)
rFile.write(bytes, 0, ch);
rFile.close();
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
注:sFullName是保存路径,in是获取的BLOB库文件
如:in = rs.getBinaryStream("content");
//一个完整的插入BLOB文件的测试类
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
private Connection conn;
public Connection getConnection() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:cxh", "cxh", "cxh");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public int insertImage(String path) throws Exception {
int i = 0;
Statement st = null;
ResultSet rs = null;
conn=this.getConnection();
conn.setAutoCommit(false);//设置数据库为不自动提交,必须的一步
st = conn.createStatement();
//先插入一个空对象,这里我调用了Empty_BLOB()函数
i = st.executeUpdate("insert into zzy_tb_mmscontent (contentid, content) values (seq_zzy_tb_mmscontent.Nextval,Empty_BLOB())");
//以行的方式锁定
rs = st.executeQuery("select content from zzy_tb_mmscontent where contentid=(select max(contentid) from zzy_tb_mmscontent) for update");
if (rs.next()) {
//得到流
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
//从得到的低级流构造一个高级流
PrintStream ps = new PrintStream(blob.getBinaryOutputStream());
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(path));
byte[] buff = new byte[1024];
int n = 0;
//从输入到输出
while ((n = bis.read(buff)) != -1) {
ps.write(buff, 0, n);
}
//清空流的缓存
ps.flush();
//关闭流,注意一定要关
ps.close();
bis.close();
}
rs.close();
st.close();
conn.close();
return i;
}
public static void main(String[] args) throws Exception {
Test test=new Test();
test.insertImage("e://ni.gif");
System.out.println("OK");
}
}
java处理图片的所有类_JAVA处理图片常用公共类相关推荐
- java escape工具类_java开发常用工具类
在Java中,,工具类定义了一组公共方法.你把你的类继承这些类或者实现这些接口,就可以使用这些类的方法了.下面给大家介绍一下十六种最常用的java开发常用工具类. 一. org.apache.comm ...
- java有哪些类_Java中常用的类有哪些?
展开全部 一. System: 1. 首先是System类,因为从一开始从接触java起,我32313133353236313431303231363533e58685e5aeb9313333 ...
- java mysql访问类_java 访问数据库公共类
package com.javaweb.mvc; import java.util.ArrayList; import java.util.List; import java.sql.*;/** * ...
- java集合的存储特征_Java集合常用类特点整理
集合的结构如下图所示: 集合的两个顶级接口分别为:Collection和Map Collection下有两个比较常用的接口分别是List(列表)和Set(集),其中List可以存储重复元素,元素是有序 ...
- java中椭圆类_Java中的Graphics2D类基本使用的教程
Java中的Graphics2D类基本使用的教程 Java语言在Graphics类提供绘制各种基本的几何图形的基础上,扩展Graphics类提供一个Graphics2D类,它拥用更强大的二维图形处理能 ...
- Java集合框架之四大接口、常用实现类,java基础面试笔试题
我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家. 扫描二维码或搜索下图红色VX号,加VX好友,拉你进[程序员面试学习交流群]免费领取.也欢迎各位一起 ...
- java导出类_java导出excel工具类
java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...
- java源程序可以有几个主类_Java源程序是由类定义组成的,每个程序可以定义若干个类,但只有一个类是主类。_学小易找答案...
[填空题]分析以下程序的执行结果 #include using namespace std; class Sample { int x; int y; public: Sample(int a,int ...
- java 面试什么是类_Java 面试题代码类收集
long temp=(int)3.9; System.out.println(temp); temp%=2; System.out.println(temp); 3 1 View Code Java里 ...
- java 由接口构造对象_Java学习笔记04——类和对象|抽象和接口|构造方法与继承...
六.Java类 1. Java中用关键字class来创建类. 2. 类中属性和方法称为类的成员;类的声明和方法要在同一个文件内,不同于C++. 3. 类的定义: [] class [extend ...
最新文章
- 聊聊抖音、奈飞、Twitch、大疆、快手、B站的多媒体关键技术
- 移动机器人平台的坐标系---map,odom,base_link
- 笔记-项目合同管理-要约、承诺和要约邀请
- 5、mybatis主配置文件之environments
- codevs 2494 Vani和Cl2捉迷藏
- nginx https http2
- 自制最小的无盘网络服务器,无盘网络终极制作2003服务器端安装准备
- 远控免杀专题(20)-GreatSCT免杀
- 利用Caffe实现mnist的数据训练
- sed,awk,grep,trap,trap,cut,tr,curl,find
- 从CVPR2019 看计算机视觉最新趋势
- 基于canvas的图片反色处理
- mysql建库图解_MySQL数据库安装图解
- 怎样通过执行命令重启电脑资源管理器,任务栏自动隐藏修复
- 父页面js监听iframe内子页面并作出响应
- 3. 乱石穿空,惊涛拍岸,卷起千堆雪。 2. 故垒西边,人道是,三国周郎赤壁。 4. 江山.... hai太乱了,我们来写程序排序输出到磁盘文件中
- Centos 7 内核文件丢失的快速修复
- 计算机网络之『路由器的配置』
- matlab中size函数使用
- 2022大厂前端面试题手册