【问题标题】:Send Image with Parameters To Server Using HttpURLConnection in android在android中使用HttpURLConnection将带有参数的图像发送到服务器
【发布时间】:2018-06-29 16:31:56
【问题描述】:

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

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'];
}

?>

最终,两个图像都成功传输,但是我无法发送/接收额外的参数。我正确收到了响应,但是在所有 php 代码中(我在这个问题中省略了一些部分),似乎没有发送/接收任何参数。

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

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

任何建议将不胜感激!

谢谢!

【问题讨论】:

  • 将位图直接压缩到数据输出流。您不需要中间 bytearrayoutputstream。
  • 嗨!感谢您的建议!我会做的!虽然这似乎不会影响我的问题!

标签: java php android post httpurlconnection


【解决方案1】:

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

request.writeBytes(twoHyphens + boundary + crlf);

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

request.writeBytes(twoHyphens + boundary + twoHyphens);

在行尾添加两个连字符。

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

【讨论】:

    猜你喜欢
    • 2017-06-30
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2013-10-16
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多