Note that there are some explanatory texts on larger screens.

plurals
  1. POSend image with HttpUrlConnection by chunks and other parameters
    primarykey
    data
    text
    <p>The thing is I'm trying to upload an image to a server. The image has to be uploaded by chunks of 256kb and I need to pass the chunks count and id with every call. I can get the total number of chunks to upload and I'm using a BufferedInputStream to get the chunks bytes. But when I finish to upload all the chunks the image showed is always corrupted.</p> <p>My code so far:</p> <pre><code>int chunkSize = 255 * 1024; final long size = mFile.length(); final long chunks = mFile.length() &lt; chunkSize? 1: (mFile.length() / chunkSize); int chunkId = 0; BufferedInputStream stream = new BufferedInputStream(new FileInputStream(mFile)); String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "RQdzAAihJq7Xp1kjraqf";// random data for (chunkId = 0; chunkId &lt; chunks; chunkId++) { URL url = new URL(urlString); // Open a HTTP connection to the URL conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(20000 /* milliseconds */); conn.setConnectTimeout(20000 /* milliseconds */); // Allow Inputs conn.setDoInput(true); // Allow Outputs conn.setDoOutput(true); // Don't use a cached copy. conn.setUseCaches(false); // Use a post method. conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); dos = new DataOutputStream( conn.getOutputStream() ); dos.writeBytes(twoHyphens + boundary + lineEnd); String param1 = ""+chunkId; String param2 = ""+chunks; String param3 = mFile.getName(); // for every param dos.writeBytes("Content-Disposition: form-data; name=\"chunk\"" + lineEnd); dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd); dos.writeBytes("Content-Length: " + param1.length() + lineEnd); dos.writeBytes(lineEnd); dos.writeBytes(param1 + lineEnd); dos.writeBytes(twoHyphens + boundary + lineEnd); // Send parameter #chunks dos.writeBytes("Content-Disposition: form-data; name=\"chunks\"" + lineEnd); dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd); dos.writeBytes("Content-Length: " + param2.length() + lineEnd); dos.writeBytes(lineEnd); dos.writeBytes(param2 + lineEnd); dos.writeBytes(twoHyphens + boundary + lineEnd); // Send parameter #name dos.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd); dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd); dos.writeBytes("Content-Length: " + param4.length() + lineEnd); dos.writeBytes(lineEnd); dos.writeBytes(param3 + lineEnd); dos.writeBytes(twoHyphens + boundary + lineEnd); // Send parameter #file dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + param4 + "\"" + lineEnd); // filename is the Name of the File to be uploaded dos.writeBytes("Content-Type: image/jpeg" + lineEnd); dos.writeBytes(lineEnd); byte[] buffer = new byte[chunkSize]; stream.skip(chunkId * chunkSize); stream.read(buffer); // dos.write(buffer, 0, bufferSize); dos.write(buffer); dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); dos.flush(); dos.close(); // read response... } </code></pre> <p>Many thanks!</p>
    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.
 

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