当前位置: 首页 > news >正文

如何将图片存到数据库(以mysql为例), 使用ORM Bee更加简单

如何将图片存到数据库

1. 创建数据库:

2. 生成Javabean

public class ImageExam implements Serializable {private static final long serialVersionUID = 1596686274309L;private Integer id;private String name;
//	private Blob image;private InputStream image; //将InputStream存入blobprivate byte[] binary1; //存byte[]private byte[] binary2blob; //将byte[]存入blob

省略相应的get,set

3. 使用ORM Bee对表时行插入数据和查询:
 

package org.teasoft.exam.bee.osql;import java.io.File;
import java.io.FileInputStream;
import java.util.List;import org.teasoft.bee.osql.api.Suid;
import org.teasoft.exam.bee.osql.entity.ImageExam;
import org.teasoft.exam.comm.Printer;
import org.teasoft.honey.osql.shortcut.BF;
import org.teasoft.honey.util.StreamUtil;/*** @author Kingstar* @since  2.1.10*/
public class ByteArrayOrBlobExam {public static void main(String[] args) throws Exception {Suid suid=BF.getSuid();ImageExam imageExam = new ImageExam();String filePath = "F://testPic.jpg";File imageFile = new File(filePath);FileInputStream inputStream = new FileInputStream(imageFile);imageExam.setImage(inputStream); //将InputStream存入blobString str = "Hello, World!";byte[] byteArray = str.getBytes();imageExam.setBinary1(byteArray);  //存byte[]imageExam.setBinary2blob(byteArray); //将byte[]存入blobsuid.insert(imageExam);ImageExam imageSelect = new ImageExam();imageSelect.setId(473684481);
//		imageSelect.setBinary1(byteArray); //不能用于过滤List<ImageExam> list=suid.select(imageSelect);Printer.printList(list);StreamUtil.inputStream2File(list.get(0).getImage(), "F://testPic2.jpg");System.out.println(StreamUtil.byteArray2String(list.get(0).getBinary1()));System.out.println(StreamUtil.byteArray2String(list.get(0).getBinary2blob()));}}

获取数据库的图片,并保存到指定位置:
    StreamUtil.inputStream2File(list.get(0).getImage(), "F://testPic2.jpg");

此实例也演示了,如何设置inputStream, byte[]到数据库;

另外,inputStream是可以设置到mysql的blob字段的

http://www.lryc.cn/news/182645.html

相关文章:

  • 【“栈、队列”的应用】408数据结构代码
  • es的nested查询
  • <一>Qt斗地主游戏开发:开发环境搭建--VS2019+Qt5.15.2
  • python:进度条的使用(tqdm)
  • Java类型转换和类型提升
  • C# 读取 Excel xlsx 文件,显示在 DataGridView 中
  • Docker02基本管理
  • Scala第十章
  • 10.4 校招 实习 内推 面经
  • 从0开始深入理解并发、线程与等待通知机制(中)
  • UE5报错及解决办法
  • 怎么通过docker/portainer部署vue项目
  • 【面试经典150 | 矩阵】旋转图像
  • 机器人制作开源方案 | 家庭清扫拾物机器人
  • C++算法 —— 动态规划(8)01背包问题
  • ASUS华硕天选4笔记本FA507NU7735H_4050原装出厂Win11系统
  • 金蝶OA server_file 目录遍历漏洞
  • read_image错误
  • 文本分词排序
  • SQL与关系数据库基本操作
  • 【2023年11月第四版教材】第18章《项目绩效域》(第一部分)
  • Docker启动Mysql
  • QScrollArea样式
  • 【gitlab】git push -u origin master 报403
  • 第二篇:矩阵的翻转JavaScript
  • 代码随想录算法训练营第五十七天 | 动态规划 part 15 | 392.判断子序列、115.不同的子序列
  • 【国漫逆袭】人气榜,小医仙首次上榜,霍雨浩排名飙升,不良人热度下降
  • 国庆中秋特辑(七)Java软件工程师常见20道编程面试题
  • 长剖与贪心+树上反悔贪心:1004T4
  • 二叉树经典例题