Note that there are some explanatory texts on larger screens.

plurals
  1. POsome kelobytes read from socket but notwrite to the file
    primarykey
    data
    text
    <p>I am at the server side, and receive Image as binary stream from a client, the server Detect that we deal with image and the size in bytes arrived to the server via previous message from the client!</p> <p>the server have a size of the file supposed to read, and stream...Eter to while loop... read from the Client Stream, and immediately write to the file, and reset the buffer to read<br> Everything Work wellm until Some kilobytes before the end of the file some kilobytes don't writen to the file... I get smaller file on the server library... (and I can see the image with small gray row at the bottom. </p> <p>here the my attempt : </p> <pre><code> /*loop*/ while (bytesRemaining!=0) { try { // read from client baos = new ByteArrayOutputStream(); bytesRemaining = sizeToread - readtotal; bytesRead = OIS.read(aByte, 0, bytesRemaining); readtotal+=bytesRead; System.out.println( ", Remaining: " +bytesRemaining); System.out.println("read at oce: "+bytesRead) ; System.out.println(++i+") have Read: "+ readtotal+", aByte:"+aByte[i-1]); try { baos.write(aByte); // write first byte to buffer try{ bos.write(baos.toByteArray(),0,bytesRead); }catch (IOException s){ System.out.println("not write to file"+s);} baos.reset(); // bos.flush(); } catch (IOException ex) { Logger.getLogger(ThrdConv.class.getName()).log(Level.SEVERE, null, ex); System.out.println("baos.write(aByte); error = "+ex ); } // if (!(OIS.available()&gt; 0))break; // until uavailabil stream } catch (EOFException eoff) { Logger.getLogger(ThrdConv.class.getName()).log(Level.SEVERE, null, eoff); System.out.println("Line 601 error= "+eoff ); break; } catch (IOException ex) { Logger.getLogger(ThrdConv.class.getName()).log(Level.SEVERE, null, ex); System.out.println("Line 612,613 error= "+ex ); } } //loop End /////////////////////////////////////////// </code></pre> <h2>note:file size: 474591</h2> <p>My prints gives:</p> <pre><code>... ... ... 458) have Read: 468992, aByte:52 , Remaining: 5599 read at oce: 1024 459) have Read: 470016, aByte:59 , Remaining: 4575 read at oce: 1024 460) have Read: 471040, aByte:-117 , Remaining: 3551 read at oce: 1024 461) have Read: 472064, aByte:-110 , Remaining: 2527 read at oce: 1024 462) have Read: 473088, aByte:-8 , Remaining: 1503 read at oce: 1024 463) have Read: 474112, aByte:-124 , Remaining: 479 read at oce: 479 464) have Read: 474591, aByte:125 , Remaining: 0 read at oce: 0 465) have Read: 474591, aByte:30 realfile: C:\wamp\www\RandomSendServer\images\1384252358431.jpg have Read: 474591 !!! URL sent Back </code></pre> <p>I get file with size 466944 istead of: 474591 .... any help</p> <p><strong>update</strong></p> <pre><code> according to answer 1 I make this change... my loop now looks: while ((bytesRead = OIS.read(aByte))&gt;1) { bos.write(aByte,0,bytesRead); i+=bytesRead; System.out.println(bytesRead+" "+i); } </code></pre> <p>the print: </p> <p>total read: 474591 realfile: C:\wamp\www\RandomSendServer\images\1384262866220.jpg URL sent</p> <p><strong>but the same problem still</strong> the loop tell it read all the 474,591 ... but when I check the file its size is: 466,944</p> <h2><strong>Final Solution</strong></h2> <pre><code> while( ( bytesRead = OIS.read(aByte))&gt;0) { fos.write(aByte,0,bytesRead); i+=bytesRead; // i is a counter if(i==sizeToread) break; // force to stop when read all the bytes in the file! } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload