【问题标题】:How to send chunk files in web api in android?如何在android的web api中发送块文件?
【发布时间】:2013-08-29 17:53:56
【问题描述】:

我正在发送大于 2mb 的文件。为了能够发送这个文件,我需要把这个文件分成每块 2mb 大小的小块 [chunks] 并按块发送。我已经将文件分成较小的块并将其发送到 Web 服务器,但问题是当我已经收到响应时,文件大小越来越大,同时逐块发送文件。但是每个块的文件大小为 2mb 所以我'我想知道为什么会发生这种情况。 预期文件长度是我发送的特定块的长度,块长度是所有已发送块的加法大小。到目前为止我得到了:

        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();
            }
        }

这是我得到的示例响应:

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

文件长度增加而不是块长度

【问题讨论】:

  • 你确定你没有混淆数字的含义吗?对我来说似乎很奇怪,块大小是文件的长度,文件大小是块的长度。
  • 我正在做的只是将文件块 1 逐个发送并以字节的形式写入数据输出流

标签: android httpurlconnection chunking dataoutputstream


【解决方案1】:
"ChunkLength":2097152,"FileLength":6291456

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

【讨论】:

  • 您是否知道响应块长度将如何增加而不是文件长度,因为我要发送的块的文件长度是 2mb 并且每次我发送块文件时块长度都会增加获取我发送的所有块的大小
  • 服务器也是你准备的吗?当您以 1MB 块上传文件时,ChunkLength 会返回什么作为响应?我认为服务器只返回 ChunkLength 字段中接收到的块的大小。
猜你喜欢
  • 2022-06-13
  • 1970-01-01
  • 2016-11-03
  • 2020-01-18
  • 2012-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-25
相关资源
最近更新 更多