Java 如何从blob在磁盘上写入映像?
我正在尝试从磁盘上的数据库创建一个映像文件。我编写了以下代码:Java 如何从blob在磁盘上写入映像?,java,Java,我正在尝试从磁盘上的数据库创建一个映像文件。我编写了以下代码: { oracle.sql.BLOB blob1 = (BLOB) rs.getBlob(1); //fillFilePath is file path File blobFile = new File(fillFilePath); String checkExe[]=fillFilePath.split("\\."); FileOutputStream outStream = n
{
oracle.sql.BLOB blob1 = (BLOB) rs.getBlob(1);
//fillFilePath is file path
File blobFile = new File(fillFilePath);
String checkExe[]=fillFilePath.split("\\.");
FileOutputStream outStream = new FileOutputStream(blobFile);
InputStream inStream = blob1.getBinaryStream();
int length = -1;
int size = blob1.getBufferSize();
byte[] buffer = new byte[size];
BufferedImage image = ImageIO.read( inStream );
System.out.println("Inside image upload");
System.out.println("Inside image jpg");
ImageIO.write(image, "JPG", outStream);
但它不起作用
请给我一些建议?试试:
BLOB image = ((OracleResultSet) rs).getBLOB("image");
blobLength = image.length();
chunkSize = image.getChunkSize();
binaryBuffer = new byte[chunkSize];
for (position = 1; position <= blobLength; position += chunkSize)
{
bytesRead = image.getBytes(position, chunkSize, binaryBuffer);
outputFileOutputStream.write(binaryBuffer, 0, bytesRead);
totbytesRead += bytesRead;
totbytesWritten += bytesRead;
}
BLOB image=((OracleResultSet)rs.getBLOB(“image”);
blobLength=image.length();
chunkSize=image.getChunkSize();
binaryBuffer=新字节[chunkSize];
对于(position=1;position blob中是什么?如果是jpeg,则根本不需要使用ImageIO,只需将字节写入磁盘。我尝试将上述代码数据写入图像文件,但它不允许该图像。
BufferedImage bi= ImageIO.read(obj.getPhoto().getBinaryStream());//photo is Blob.
File outputfile = new File("folderInYourProject\\"+nameVar+".jpg");
ImageIO.write(bi, "jpg", outputfile);