1

我正在开发一个可以将数据上传到 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());
        }
4

0 回答 0