如何获取文件的内容长度我要发送?

时间:2013-08-14 02:56:26

标签: android httpurlconnection content-length

我正在开发一个可以将数据上传到Web服务器的应用程序。但我不知道我要把它放在什么内容上。我试图获取我的文件大小以及我要上传的其他文本数据,但这不正确。任何人都知道我应该把我的内容长度放在什么位置?

          for (chunkId = 0; chunkId < 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");

                 String encoded = Base64.encodeToString((_username+":"+_password).getBytes(),Base64.NO_WRAP); 
                 conn.setRequestProperty("Authorization", "Basic "+encoded); 
                 conn.setRequestProperty("Connection", "Keep-Alive");
                 //conn.setChunkedStreamingMode(mychunkSize);
                 conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
                 conn.setRequestProperty("Content-Length", "**I don't know what I'm going to put here**");
                 conn.connect();

                 dos = new DataOutputStream( conn.getOutputStream() );
                 //dos.writeBytes("Content-Length: " + String.valueOf(size) + lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);



                // Send parameter #file
                dos.writeBytes("Content-Disposition: form-data; name=\"fieldNameHere\";filename=\"" + mFile.getName() + "\"" + lineEnd); // filename is the Name of the File to be uploaded
                dos.writeBytes("Content-Type: image/jpeg" + lineEnd);
                dos.writeBytes(lineEnd);


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


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


                bytesAvailable = stream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];
                // read file and write it into form...
                bytesRead = stream.read(buffer, 0, bufferSize);


                while (bytesRead > 0) {
                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = stream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = stream.read(buffer, 0, bufferSize);
                    Log.i("BytesAvailable", String.valueOf(bytesAvailable));
                    Log.i("bufferSize", String.valueOf(bufferSize));
                    Log.i("buffer", String.valueOf(buffer));
                }

                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                // close streams

                stream.close();
                dos.flush();
                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());
        }

0 个答案:

没有答案