简体   繁体   English

如何将Java中的图片保存为多个blob?

[英]How to save a picture in Java into Multiple blob?

As I described in the title, I tried to access a picture file several times using an InputStream , then save it to blob type database but the result is null for the second, third, and so on. 正如我在标题中所描述的那样,我尝试使用InputStream多次访问图片文件,然后将其保存到blob类型数据库,但结果为第二个,第三个,等等为null。 Here is the snippet of the code: 这是代码的片段:

File image1 = new File("src/template1.png").getAbsoluteFile();
File image2 = new File("src/template2.png").getAbsoluteFile();
InputStream in1 = new FileInputStream(image1);
InputStream in2 = new FileInputStream(image2);
long length1 = image1.length();
long length2 = image2.length();
pstmt.setString(1, jTextField18.getText().toUpperCase());
pstmt.setBlob( 2, in1, length1 );
pstmt.setBlob( 3, in1, length1 );
pstmt.setBlob( 4, in1, length1 );
pstmt.setBlob( 5, in1, length1 );
pstmt.setBlob( 6, in1, length1 );
pstmt.setBlob( 7, in2, length2 );
Pstmt.setBlob( 8, in2, length2 );

then the result is: 然后结果是:

YR/21
[BLOB - 5.6 KiB]
[BLOB - 0 B]
[BLOB - 0 B]
[BLOB - 0 B]
[BLOB - 0 B]
[BLOB - 5.5 KiB]
[BLOB - 0 B]

I tried to use new InputStream like this code and another error occured. 我尝试使用像这段代码的新InputStream并发生了另一个错误。 It says: Row size too large(>8126). 它说:行大小太大(> 8126)。 Changing some columns to TEXT or BLOB or using ROW_FORMAT=DINAMIC or ROW_FORMAT=COMPRESSED may help. 将某些列更改为TEXT或BLOB或使用ROW_FORMAT = DINAMIC或ROW_FORMAT = COMPRESSED可能会有所帮助。 In current row format, BLOB prefix of 768 bytes is stored inline. 在当前行格式中,760字节的BLOB前缀以内联方式存储。

pstmt.setBlob( 2, in1, length1 );
image1 = new File("src/template1.png").getAbsoluteFile();
in1 = new FileInputStream(image1);
pstmt.setBlob( 3, in1, length1 );
image1 = new File("src/template1.png").getAbsoluteFile();
in1 = new FileInputStream(image1);
pstmt.setBlob( 4, in1, length1 );

I think the problem is answered and change into another problem. 我认为问题得到解决并转变为另一个问题。 Thanks for the answer 谢谢你的回答

The first call consumes the stream, and the subsequent calls get no data. 第一个调用消耗流,后续调用不获取数据。 You need a separate InputStream for each call. 每次调用都需要一个单独的InputStream。

"Is there any way to save a picture to multiple blob?" “有没有办法将图片保存到多个blob?”

Yes, but your code will only work for small images. 是的,但您的代码仅适用于小图片。 Instead, have a parent reference for the image and a children for the actual image, which will have the blobs. 相反,有一个图像的父引用和实际图像的子代,它们将具有blob。 Make the children small and save only a chunk of the image to every one of them. 让孩子们变小,只为每一个孩子保存一大块图像。

This will help you manage the space and it will be easier to back up. 这将有助于您管理空间,并且更容易备份。

For a reference on how to read a file in chunks, take a look here: http://www.javapractices.com/topic/TopicAction.do?Id=245 有关如何以块的形式读取文件的参考,请查看此处: http//www.javapractices.com/topic/TopicAction.do?Id = 245

Save the chunk of the image to the database using the while-loop that reads the file. 使用读取文件的while循环将图像块保存到数据库。

It could be a lengthy operation to save the file, so you could have a flag on the parent defining if the children were all loaded or not. 保存文件可能是一个冗长的操作,因此您可以在父项上有一个标志,用于定义子项是否全部加载。 Thus preventing other threads from reading incomplete images. 从而防止其他线程读取不完整的图像。

If you are using MySQL, there's this tutorial on how to read and write images: http://zetcode.com/db/mysqljava/ 如果您使用的是MySQL,那么本教程将介绍如何读取和写入图像:http: //zetcode.com/db/mysqljava/

Have fun. 玩得开心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM