简体   繁体   中英

How to send chunk files in web api in android?

I'm sending a files that is greater than 2mb. In able to send this file I need to divide this file into smaller pieces with the size of 2mb per pieces[chunks] and send it by chunk. I already divided the files into smaller chunks and send it to web server, but the problem is when I already getting the response the file size is getting bigger while sending the file chunk by chunk.But the file size of every chunk is 2mb so I'm wondering why this happening. The expected file length is the length of the specific chunk that I send and the chunk length is the addition size of all the chunk that is already send. Here I got so far:

        File mFile = new File(samplefile);

        int mychunkSize = 2048 * 1024;
        final long size = mFile.length();
        final long chunks = size < mychunkSize? 1: (mFile.length() / mychunkSize);

        int chunkId = 0;
        int ordername = 0;

        int bytesRead = 0, bytesAvailable, bufferSize;


        byte[] buffer;

        int maxBufferSize = 2 * 1024 * 1024;

        if (size > maxBufferSize){ //if file size is greater than 2mb
            try {
                splitFiles = ChunkFiles.splitFile(mFile);
                String chunkedfiles="";
                int i = 0;

                for (i=0; i < splitFiles.size(); i++) {

                    chunkedfiles = splitFiles.get(i);


                    try {
                        File theChunkFiles = new File(chunkedfiles);
                        FileInputStream stream = new FileInputStream(theChunkFiles);

                        bytesAvailable = stream.available();
                        bufferSize = Math.min(bytesAvailable, maxBufferSize);
                        buffer = new byte[bufferSize];
                        int bufferlength = buffer.length;

                         // read file and write it into form...
                         bytesRead = stream.read(buffer, 0, bufferSize);
                        ordername++;

                        String lineEnd = "\r\n";
                        String twoHyphens = "--";
                        String boundary =  "-------------------------acebdf13572468";// random data


                         String param3 = mFile.getName();
                         String param4 = samplefile;
                         String chunkFileName = theChunkFiles.getName();

                             URL url = new URL(urlString);

                             // Open a HTTP connection to the URL
                             conn = (HttpURLConnection) url.openConnection();

                             // 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");

                             String encoded = Base64.encodeToString((_username+":"+_password).getBytes(),Base64.NO_WRAP); 
                             conn.setRequestProperty("Authorization", "Basic "+encoded);
                             conn.setRequestProperty("Connection", "Keep-Alive");
                             conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

                             conn.setChunkedStreamingMode(0);


                             dos = new DataOutputStream( conn.getOutputStream() );

                             dos.writeBytes(twoHyphens + boundary + lineEnd);


                            dos.writeBytes("Content-Disposition: form-data; name=\"fieldNameHere\";filename=\"" + chunkFileName + "\"" + lineEnd); // filename is the Name of the File to be uploaded
                            dos.writeBytes("Content-Type: application/octet-stream" + lineEnd);
                            dos.writeBytes(lineEnd);

                            while (bytesRead > 0) {
                                dos.write(buffer, 0, bufferSize);
                                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                                bytesRead = stream.read(buffer, 0, bufferSize);
                            }


                                var = bufferSize;

                            dos.writeBytes(lineEnd);
                            dos.writeBytes(twoHyphens + boundary + lineEnd);


                            // Send parameter #chunks
                            dos.writeBytes("Content-Disposition: form-data; name=\"chunk\"" + lineEnd);

                            dos.writeBytes(lineEnd);
                            dos.writeBytes(chunkId + lineEnd);
                            dos.writeBytes(twoHyphens + boundary + lineEnd);


                            // Send parameter #name
                            dos.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);

                            dos.writeBytes(lineEnd);
                            dos.writeBytes("ForFiddlerTest19VIDEO_20130711_151315_-2073548175.mp4" + lineEnd);



                            // send multipart form data necesssary after file data...
                            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                            // close streams

                            chunkId++;


                             is = conn.getInputStream();

                          // retrieve the response from server
                          int ch;

                          StringBuffer b =new StringBuffer();
                          while( ( ch = is.read() ) != -1 ){ b.append( (char)ch ); }
                          String s=b.toString();
                          Log.i("Response",s);
                         //stream.close();
                         is.close();
                         dos.close();



                    } catch (MalformedURLException ex) {
                        System.out.println("From CLIENT REQUEST:" + ex);
                    }catch (IOException ioe) {
                        System.out.println("From CLIENT REQUEST:" + ioe);
                    }catch (Exception e) {
                        Log.e("From CLIENT REQUEST:", e.toString());
                    }



                }
                dos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

Here is the sample Response I've got:

{"Filename":"ForFiddlerTest19VIDEO_20130711_151315_-2073548175.mp4","ChunkId":2,"ChunkLength":2097152,"FileLength":6291456}

The file length is incrementing instead of the chunk length

"ChunkLength":2097152,"FileLength":6291456

块的长度是恒定的-2 MB并且服务器返回的文件长度在增加,因为服务器可能将块连接到单个文件中-在最终文件的末尾附加每个新块,因此对于每个请求,您都会得到新的更大的文件长度作为响应,而块长度是恒定的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM