Invalid JPEG file structure: Two SOI markers error?

This is the image that causes the error.

I have this problem and I tried everything I know, but nothing worked. I am trying to send multiple images (one image at a time) from a database to a client application via a socket, sometimes everything works fine, but sometimes it raises the error "incorrect JPEG file structure: two SOI errors"?

client side:

for(User user : users){
    int cmpt=0;

    byteToread =in.readInt();
    bytesOut=new ByteArrayOutputStream();
    bos = new BufferedOutputStream(bytesOut);    

    while (byteToread >cmpt) {
        cmpt = in.read(dataEntre);
        bos.write(dataEntre, 0, cmpt);
        bos.flush();
        byteToread-=cmpt;
    } 
    BufferedImage bi = ImageIO.read(new ByteArrayInputStream(bytesOut.toByteArray()));

    user.setPhoto(new ImageIcon(bi));
    System.out.println("------------------end");
}
bos.close();
bytesOut.close();

server side:

InputStream input =null;

Statement myStmt = null;
ResultSet myRs = null;
BufferedInputStream bis=null;
try {
    myStmt = Conn.createStatement();
    myRs = myStmt.executeQuery("select Photo from users order by Name");
    byte[] buffer; 

    int k =1;

    while (myRs.next()) {
        input=myRs.getBinaryStream("Photo");
        bis = new BufferedInputStream(input);
        buffer = new byte[1024];

        try {
            int byteToread = 0;
            int cmpt=0;

            byteToread=input.available();
            out.writeInt(byteToread);
            out.flush(); 

            int i=0;  
            while (byteToread>cmpt) {
                cmpt = bis.read(buffer);
                out.write(buffer, 0, cmpt);
                out.flush();
                byteToread -= cmpt;
            } 
        } catch (IOException ex) {
            return ;
        }
    }
+4
source share
2 answers

Do not use available()as a measure of input length. There is a special warning in Javadok. This is not for anything.

Your copy paths should look like this:

while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}
+4
source

First of all: the error you are facing:

JPEG StartOfImage, : FFD8 SOI. , 1 + 2, , .

, :

byteToread =in.readInt();
bytesOut=new ByteArrayOutputStream();
bos = new BufferedOutputStream(bytesOut);     
while (byteToread >cmpt) {
    cmpt = in.read(dataEntre);
    bos.write(dataEntre, 0, cmpt);
    bos.flush();
    byteToread-=cmpt;
} 

, , , 100 . - 100 50, 30, 20 .

:

iteration 1:
during while:   
    bytesToRead: 100
    cmpt: 0
at the end of the loop:
    bytesToRead: 50
    cmpt: 50
iteration 2:
during while:   
    bytesToRead: 50
    cmpt: 50

byteToRead (50) > cmpt (50), , 50 .

, , - :

bytesToread = in.readInt();
bytesOut=new ByteArrayOutputStream();
bos = new BufferedOutputStream(bytesOut);     
while (byteToread > 0) {
    cmpt = in.read(dataEntre);
    bos.write(dataEntre, 0, cmpt);
    bos.flush();
    byteToread-=cmpt;
}

, ( , ). , . , , , . ( )

( )

byteToread=input.available();

, db, .

()

, InputStream , . , .

, , ( ), :

1: 1 (, 200 ). 2: (200) 3: 200 (, 150). (- ) 4: 2 (, 300 ) 5: (300) 6: N .

1: image1.length. 200. 2: 200 . 150 1, , 150 1, 2, 46 2.

, 1, 2 jpg. ( 46 image2 image2 afterall)

, , . , . .

+2

All Articles