Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. It says: Row size too large(>8126). Changing some columns to TEXT or BLOB or using ROW_FORMAT=DINAMIC or ROW_FORMAT=COMPRESSED may help. In current row format, BLOB prefix of 768 bytes is stored inline.

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

like image 995
cappucino Avatar asked Apr 13 '26 18:04

cappucino


1 Answers

The first call consumes the stream, and the subsequent calls get no data. You need a separate InputStream for each call.

like image 167
jtahlborn Avatar answered Apr 15 '26 08:04

jtahlborn