如何在不解析错误的情况下用Java发送多部分表单数据?

时间:2017-10-01 05:43:20

标签: java android

我正在尝试从Android应用程序向我的服务器API发送一些多部分表单数据。这是我的代码,我面临解析问题。我发布了三个值。其中两个是文本值,一个是文件。在这三个中,填充文本值无法正确解析

我可以在代码中修复什么才能正确发送所有值?

try
    {
            Log.e(Tag);

            // Open a HTTP connection to the URL
            HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
           // conn.setConnectTimeout();
           // conn.setReadTimeout();
            // 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);

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

           // dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"infill\""+ lineEnd);
            dos.writeBytes(lineEnd);
            dos.writeInt(infill_);
            dos.writeBytes(lineEnd);

        dos.writeBytes("Content-Disposition: form-data; name=\"material-type\""+ lineEnd);
            dos.writeBytes(lineEnd);
            dos.writeInt(materialType);
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + lineEnd);

            dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + iFileName +"\"" + lineEnd);
            dos.writeBytes(lineEnd);

           // Log.e(Tag,"Headers are written");

            // create a buffer of maximum size
            int bytesAvailable = fileInputStream.available();

            int maxBufferSize = 1024;
            int bufferSize = Math.min(bytesAvailable, maxBufferSize);
            byte[ ] buffer = new byte[bufferSize];

            // read file and write it into form...
            int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0)
            {
                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable,maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0,bufferSize);
            }
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // close streams
            fileInputStream.close();

            dos.flush();

          Log.i("STL File Sent, Response code: "+String.valueOf(conn.getResponseCode()));
          Log.i("STL File Sent, Response message : "+String.valueOf(conn.getResponseMessage()));

            InputStream is = conn.getInputStream();

            // retrieve the response from server
            int ch;

            StringBuffer b =new StringBuffer();
            while( ( ch = is.read() ) != -1 ){ b.append( (char)ch ); }
            String response=b.toString();
            Log.i("STL Response + "+response);
            dos.close();

            // Set cost and time
            getDataFromJsonResponse(response);          
    }

1 个答案:

答案 0 :(得分:0)

   dos.writeInt(infill_);

这应该以文本形式发送,而不是二进制。 HTML是一种文本协议。没理由真的使用DataOutputStream

 // create a buffer of maximum size
 int bytesAvailable = fileInputStream.available();
 int maxBufferSize = 1024;
 int bufferSize = Math.min(bytesAvailable, maxBufferSize);
 byte[ ] buffer = new byte[bufferSize];

 // read file and write it into form...
 int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

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

你不需要这一切。只是:

byte[] buffer = new byte[8192];
int count;
while ((count = fileInputStream.read(buffer)) > 0)
{
    dos.write(buffer, 0, count);
}

available()的正确用法很少,而且这不是其中之一。