Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy byte array size has 4 bytes offset when uploading and downloading the array?
    text
    copied!<p>I'm trying to upload image from client to server and get it back to client.</p> <p><strong>Step 1:</strong> uploading image from client to server</p> <pre><code>HttpResponse httpResponse; HttpPost postRequest = new HttpPost(uploadUrl); MultipartEntity entity = new MultipartEntity(); entity.addPart("image", new ByteArrayBody(bitmapArray, "image/jpeg", "image")); postRequest.setEntity(entity); HttpClient httpClient = getHttpClient(); httpResponse = httpClient.execute(postRequest); // bitmapArray.length = 1000 </code></pre> <p><strong>Step 2:</strong> getting the image from client</p> <pre><code>ServletFileUpload upload = new ServletFileUpload(); FileItemIterator iter = upload.getItemIterator(request); FileItemStream imageItem = iter.next(); InputStream imgStream = imageItem.openStream(); </code></pre> <p><strong>Step 3:</strong> sending the image back to client</p> <pre><code>response.setContentType("image/jpeg"); byte[] bArray = IOUtils.toByteArray(imgStream); response.getOutputStream().write(bArray); // bArray.length = 1000 </code></pre> <p><strong>Step 4:</strong> getting the image back from reponse</p> <pre><code>httpResponse = httpClient.execute(postRequest); InputStream is = httpResponse.getEntity().getContent(); byte[] bytes = IOUtils.toByteArray(is); // bytes.length = 1004 </code></pre> <ul> <li>Steps 1 and 4, 2 and 3 are Sequential I separated them to show the flow of them image.</li> </ul> <p>Now suppose that I'm trying to send 1000 bytes image, Before I send the image array at step 1 the length of bitmapArray is 1000.<br> At step 3 after I fetched the image stream and converted it to bArray with IOUtils the length of bArray is remained 1000 so I'm guessing that the image is transferd Ok.<br> <strong>But</strong> in step 4 after I sent the image stream from server to client bytes length is 1004.<br> What am I doing wrong here? </p>
 

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