`
liujianguangaaa
  • 浏览: 235165 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

Blob、InputStream、byte 互转

    博客分类:
  • java
阅读更多

在我们的程序开发当中,经常会用到java.sql.Blob、byte[]、InputStream之间的相互转换,但在JDK的API当中,又没有直接给我们提供可用的API,下面的程序片段主要就是实现它们之间互换的util.

一、byte[]=>Blob

我们可以通过Hibernate提供的表态方法来实现如:

org.hibernate.Hibernate.Hibernate.createBlob(new byte[1024]);

二、Blob=>byte[]

目前没有找到好一点的API提供,所以只能自已来实现。示例如下:

/**
* 把Blob类型转换为byte数组类型
* @param blob
* @return
*/
private byte[] blobToBytes(Blob blob) {

BufferedInputStream is = null;

try {
is = new BufferedInputStream(blob.getBinaryStream());
byte[] bytes = new byte[(int) blob.length()];
int len = bytes.length;
int offset = 0;
int read = 0;

while (offset < len && (read = is.read(bytes, offset, len - offset)) >= 0) {
offset += read;
}
return bytes;
} catch (Exception e) {
return null;
} finally {
try {
is.close();
is = null;
} catch (IOException e) {
return null;
}
}
}

三、InputStream=>byte[]

private byte[] InputStreamToByte(InputStream is) throws IOException { 

ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); 
int ch; 
while ((ch = is.read()) != -1) { 
bytestream.write(ch); 

byte imgdata[] = bytestream.toByteArray(); 
bytestream.close(); 

return imgdata; 


四、byte[] => InputStream

byte[]到inputStream之间的转换很简单:InputStream is = new ByteArrayInputStream(new byte[1024]); 

五、InputStream => Blob

可通过Hibernate提供的API:Hibernate.createBlob(new FileInputStream(" 可以为图片/文件等路径 "));

六、Blob => InputStream

Blog转流,可通过提供的API直接调用:new Blob().getBinaryStream();

以上片段可作为读者参考

分享到:
评论

相关推荐

    blob,将byte二进制转成pdf

    标题中的“blob,将byte二进制转成pdf”指的是在IT领域中处理二进制数据(Blob)并将其转换为PDF文档的过程。Blob在计算机科学中通常代表Binary Large Object,用于存储大块非结构化的数据,如图像、音频、视频或者在...

    Java 类型相互转换byte[]类型,Blob类型详细介绍

    在Java编程中,数据存储和传输常常涉及到不同类型的数据转换,特别是在数据库操作中,与二进制大数据相关的类型如`byte[]`(字节数组)和`Blob`(Binary Large Object)之间的转换尤为常见。本篇文章将详细讲解如何...

    Hibernate读取blob字段

    byte[] bytes = new byte[(int) image.getImageData().length()]; is.read(bytes); FileOutputStream fos = new FileOutputStream("image.jpg"); fos.write(bytes); fos.close(); transaction.commit(); ...

    java将图片写入数据库,并读出来(blob clob)

    byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); } fos.close(); is.close(); } ``` 6. **关闭资源** 记得在完成操作...

    对blob大数据的操作——图片上传与显示*(自己实践可用)

    在图片显示过程中,我们可以使用HQL语句来获取数据库中的Blob数据,然后将其转换成InputStream类型。最后,我们可以在Struts.xml配置文件中配置返回的图片结果。 后期收尾 在最后,我们需要对Service层添加事务...

    java读取大字段blob类型

    4. **读取BLOB数据**:通过`ResultSet`获取到`BLOB`对象,并将其转换为`InputStream`进行读取。 ```java if (rs.next()) { oracle.sql.BLOB content = (oracle.sql.BLOB) rs.getBlob("content"); ...

    Hibernate对Blob,Clob的操作

    InputStream is = blob.getBinaryStream(); // 处理输入流,例如写入文件 Clob clob = entity.getTextData(); Reader reader = clob.getCharacterStream(); // 读取字符流,例如转换为字符串 ``` 值得注意的是,...

    oracle中读取blob字段.doc

    InputStream inStream = imgBlob.getBinaryStream(); byte[] buf = new byte[10240]; int len; while ((len = inStream.read(buf)) &gt; 0) { outStream.write(buf, 0, len); } inStream.close(); outStream....

    Oracle blob字段上传下载

    byte[] b = new byte[blob.getBufferSize()]; int len; while ((len = fin.read(b)) != -1) { outStream.write(b, 0, len); } fin.close(); outStream.flush(); outStream.close(); } rs.close(); st....

    BLOB字段处理

    然后,通过`getBinaryStream`方法将BLOB转换为InputStream,这样就可以将数据逐块读取到缓冲区中,并最终写入到目标文件中。 ### 总结 处理BLOB字段时,确保使用适当的流操作和错误处理机制是非常重要的。使用`...

    Hibernate对BLOB CLOB操作

    为了实际读取和写入BLOB和CLOB,你需要从文件系统读取数据,然后将其转换为InputStream或Reader,再使用Hibernate提供的方法。例如,你可以使用FileInputStream读取文件,然后将它转换为BLOB,类似地,使用...

    JDBC中操作Blob、Clob等对象

    InputStream blobStream = blob.getBinaryStream(); byte[] imageData = IOUtils.toByteArray(blobStream); } ``` 通过以上的示例可以看出,操作Blob和Clob数据实际上与处理普通的字符串或整型数据类似,只需要...

    Hiberante读取BLOB数据类型.

    InputStream is = doc.getContent().getBinaryStream(); byte[] data = new byte[(int) doc.getContent().length()]; is.read(data); // 处理data数组,例如写入文件或进行其他处理 is.close(); tx.commit...

    使用JDBC4.0操作Oracle中BLOB类型的数据方法

    这段代码从数据库中查询BLOB字段,获取InputStream,然后将数据写入一个新的文件。注意,为了提高性能,这里使用了BufferedInputStream和BufferedOutputStream进行缓冲操作。 在实际应用中,确保正确处理异常并关闭...

    在Oracle中存取BLOB对象实现文件的上传和下载.txt

    byte[] b = new byte[blob.getBufferSize()]; int len = 0; while ((len = fin.read(b)) != -1) { outStream.write(b, 0, len); } fin.close(); outStream.flush(); outStream.close(); con.commit(); } ...

    oracle中的Blob和Clob区别

    InputStream inStream = blob.getBinaryStream(); byte[] data = new byte[inStream.available()]; inStream.read(data); inStream.close(); } inStream.close(); con.commit(); con.close(); ``` #### ...

    java中读取ORACLE中BLOB字段存储的图片

    InputStream inStream = blob.getBinaryStream(); try { // 获取图片数据长度 long nLen = blob.length(); int nSize = (int) nLen; data = new byte[nSize]; inStream.read(data); inStream.close(); } ...

    java对oracle数据库中blob字段的处理

    byte[] fileContent = blobData.getBytes(1, (int)blobData.length()); // 这里可以将fileContent写入文件或其他操作 } rs.close(); stmt.close(); conn.close(); } } ``` #### 2. 写入BLOB字段 向Oracle...

    oracle_java_blob

    InputStream is = blob.getBinaryStream(); Files.copy(is, Paths.get("recovered_" + mp3File.getName()), StandardCopyOption.REPLACE_EXISTING); } } } ``` 总结,通过上述步骤,我们成功地利用Java将MP3...

Global site tag (gtag.js) - Google Analytics