在 android 中使用 HttpURLConnection 将带有参数的图像发送到服务器

Send Image with Parameters To Server Using HttpURLConnection in android

我正在尝试通过我的 android 应用程序将两张图片连同一些参数上传到服务器。在网上搜索并按照 here and 以及其他来源的说明进行操作后,我得到了以下代码:

String boundary = "***" + System.currentTimeMillis() + "***";
String twoHyphens = "--";
String crlf = "\r\n";
String output = "";
try {
            HttpURLConnection httpUrlConnection = null;
            URL url = new URL(myUrl);
            httpUrlConnection = (HttpURLConnection) url.openConnection();
            httpUrlConnection.setUseCaches(false);
            httpUrlConnection.setDoInput(true);
            httpUrlConnection.setDoOutput(true);
            httpUrlConnection.setRequestMethod("POST");

            httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
            httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
            httpUrlConnection.setRequestProperty("ENCTYPE", "multipart/form-data");
            httpUrlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);


            DataOutputStream request = new DataOutputStream(httpUrlConnection.getOutputStream());
            request.writeBytes(twoHyphens + boundary + crlf);

            // Convert and add first image
            ByteArrayOutputStream bao1 = new ByteArrayOutputStream();
            params[0].compress(Bitmap.CompressFormat.JPEG, 100, bao1);
            byte[] ba1 = bao1.toByteArray();


            request.writeBytes("Content-Disposition: form-data; name=\"image1\";filename=\"image1\"" + crlf);
            request.writeBytes(crlf);
            request.write(ba1);
            request.writeBytes(crlf);
            request.writeBytes(twoHyphens + boundary + crlf);

            // Convert and add second image
            ByteArrayOutputStream bao2 = new ByteArrayOutputStream();
            params[1].compress(Bitmap.CompressFormat.JPEG, 100, bao2);
            byte[] ba2 = bao2.toByteArray();

            request.writeBytes("Content-Disposition: form-data; name=\"image2\";filename=\"image2\"" + crlf);
            request.writeBytes(crlf);
            request.write(ba2);
            request.writeBytes(crlf);
            request.writeBytes(twoHyphens + boundary + crlf);

            request.writeBytes("Content-Disposition: form-data; name=\"username\"" + crlf);
            request.writeBytes(crlf);
            request.writeBytes(username);
            request.writeBytes(crlf);
            request.writeBytes(twoHyphens + boundary + twoHyphens);

            request.writeBytes("Content-Disposition: form-data; name=\"datestr\"" + crlf);
            request.writeBytes(crlf);
            request.writeBytes(timeStampString);
            request.writeBytes(crlf);
            request.writeBytes(twoHyphens + boundary + twoHyphens);

            request.flush();
            request.close();

            int responseCode = httpUrlConnection.getResponseCode();
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                InputStream responseStream = new BufferedInputStream(httpUrlConnection.getInputStream());
                BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream, Charset.forName("UTF-8")));

                String line;
                while ((line = responseStreamReader.readLine()) != null) {
                    output = line;
                    Log.d(TAG, line);
                }
                responseStreamReader.close();
            }
            httpUrlConnection.disconnect();

            if (output == "") {
                httpResultsReturned = false;
            } else {
                httpResultsReturned = true;
            }

        } catch (ProtocolException e) {
            e.printStackTrace();
            return "failed";
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "failed";
        } catch (IOException e) {
            e.printStackTrace();
            return "failed";
        }

在服务器端,我尝试按如下方式访问数据:

<?php

if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $image1 = $_FILES['image1']['name'];
    $image2 = $_FILES['image2']['name'];
    $datestr= $_POST['datestr'];
    $username= $_POST['username'];
}

?>

最终,两张图片都成功传输了,但是我不能send/receive额外的参数。我正确地收到了回复,但是在所有 php 代码中(我在这个问题中省略了一些部分),参数的 none 似乎是 sent/received.

问题中,AndroSco 分享了对他有用的解决方案,但在他的 php 文件中,他似乎只访问图像而不访问参数...

由于我在这个领域没有很多经验,我相信可能有一些非常明显的事情我 incorrectly/don 根本没有做!

如有任何建议,我们将不胜感激!

谢谢!

经过多次挫折,我发现了代码中的错误。在传输的消息中导入了两个图像后,当我想导入其他参数时,我写错了边界。而不是添加:

request.writeBytes(twoHyphens + boundary + crlf);

最后换了一行,我这样写:

request.writeBytes(twoHyphens + boundary + twoHyphens);

在行尾添加两个连字符。

twoHyphens 替换为 crlf 后,一切正常!