做外贸在哪个网站比较好,wordpress 安卓接口,做产品展示网站,行业关键词搜索量排名目录 1.varbinary(max)的说明
2.图片显示
3.总结 1.varbinary(max)的说明
varbinary(max) 是一种SQL Server数据库字段类型#xff0c;用于存储二进制数据#xff0c;可以存储最大长度的二进制数据。以下是关于 varbinary(max) 的说明#xff1a; 存储容量: 可以存储最大…目录 1.varbinary(max)的说明
2.图片显示
3.总结 1.varbinary(max)的说明
varbinary(max) 是一种SQL Server数据库字段类型用于存储二进制数据可以存储最大长度的二进制数据。以下是关于 varbinary(max) 的说明 存储容量: 可以存储最大长度为 2^31-1 字节即 2,147,483,647 字节这使得它非常适合存储大型二进制数据例如图片、音频或视频文件等。 用途: 主要用于存储不需要字符集或排序规则的二进制数据通常是不需要进行文本排序或比较的数据。 与 varbinary 的区别: varbinary(max) 可以存储超过 8,000 字节的数据而 varbinary 有大小限制最大长度为 8,000 字节。 数据处理: 在数据库中存储的二进制数据可以是任何类型的文件或数据流通过数据库管理系统可以进行存储、检索和管理。
总结而言varbinary(max) 是SQL Server中用于存储大型二进制数据的灵活且高容量的字段类型适合于需要存储大型文件或二进制数据流的场景。
2.图片显示
①使用mybatis查询二进制数据
Object selImageInfo(Param(docId) Integer docId, Param(id) Integer id, Param(docType) DocTypeEntity docType, Param(dbName) String dataIndex); select idselImageInfo resultTypejava.lang.ObjectSELECTFile_Images.fi_ImageFROM ${docType.tableName},File_ImagesWHERE ${docType.tableName}.${docType.uniqueKey} #{id}and doc_id #{docId}and ${docType.tableName}.${dbName} File_Images.fi_Id/select
查询结果返回Object类型网上有说使用byte[]作为返回值我使用的时候报错
[B cannot be cast to [Ljava.lang.Object;表示尝试将一个字节数组字节数组在Java中表示为byte[]转换成Object数组但这是不兼容的转换。
②将字节数组转换为base64格式的字符串
Object image mapper.selImageInfo(docId, id, docType, column.getDataIndex());String base64Encoded Base64.getEncoder().encodeToString((byte[])image);// ByteArrayInputStream bis new ByteArrayInputStream((byte[])image);// BufferedImage imageInfo ImageIO.read(bis);//// // 缩放比例为0.5// double scale 0.5;//// int targetWidth (int) (imageInfo.getWidth() * scale);// int targetHeight (int) (imageInfo.getHeight() * scale);//// BufferedImage resizedImage new BufferedImage(targetWidth, targetHeight, imageInfo.getType());// Image img imageInfo.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);// resizedImage.createGraphics().drawImage(img, 0, 0, null);//// // 将图片转换为Base64// ByteArrayOutputStream baos new ByteArrayOutputStream();// ImageIO.write(resizedImage, jpg, baos);// byte[] imageBytes baos.toByteArray();// String base64String Base64.getEncoder().encodeToString(imageBytes);
将sql返回object变量强转为byte数组然后转换为base64格式的字符串。
后面注释的代码的进行图片的缩放处理也可以在前端控制图片的显示大小。
③显示图片
html a-modal v-model:visiblevisible okhandleOk :hide-canceltrue ok-text关闭 width50%div styleheight: 500px;img classcentered-img :srcimageSrc altImage from database //div/a-modal
js
const cellClick async (record: any, columnInfo: any, ev: any) {
// 当点击的列信息为图片类型并且图片不为空时则展示图片if(columnInfo.colType 16 record[columnInfo.dataIndex]){// 查询图片信息const reqBody {docId:prop.docId,id:record.id,column:columnInfo}const res await getImage(reqBody)imageSrc.value data:image/jpg;base64, resvisible.value true}
}
css
.centered-img {display: block; /* 确保图像作为块级元素显示 */margin: 0 auto; /* 设置左右外边距为自动实现水平居中 */width: 90%;height: 90%;
}
将 data:image/jpg;base64,和后端传递的base64格式的字符串进行拼接设置到img的src属性中然后设置css控制显示图片的大小
3.总结
imageIO的使用
Java ImageIO 类详解-CSDN博客