Android MultipartEntity POST 接收除了 FileBody 之外的一切

4

在阅读了许多问题和答案后,我决定使用MultipartEntity。这个回答肯定是最好的:https://dev59.com/enA85IYBdhLWcg3wHvs0#2937140

我已经实现了它:

private class SendImage extends AsyncTask<String, Void, Void> {
    String sresponse;

    @Override
    protected Void doInBackground(String... params) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(
                "http://taras.xin.com.pl/loadimage.php");
        InputStream in = null;
        try {
            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            Log.i("MultyPart", params[0]);
            File file = new File(params[0]);
            Log.i("MultyPartFILE", file.toString());
            ContentBody fb = new FileBody(file, "image/jpeg");
            Log.i("MultyPartFB", fb.toString());
            entity.addPart("text", new StringBody(
                    "This text was added to posrt"));
            entity.addPart("word", new StringBody(
                    "This text was added to post"));
            entity.addPart("file", fb);
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost,
                    localContext);
            in = response.getEntity().getContent();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            in.close();
            sresponse = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                text.setText(sresponse);
            }
        });

        super.onPostExecute(result);
    }
}

而我的loadimage.php文件:

    <?php
     var_dump($_POST);
     var_dump($_FILE);
      ?>

我收到了两个字符串通过$_POST,但是没有FileBody(文件)的影子。 $_FILE返回null。

确切地说,我已经下载了httpcomponents-client-4.2.2-bin,提取了httpmime-4.2.2.jar。

1个回答

0

尝试仅获取您正在发送的文件
$file = $_FILES ['file'];


var_dump($_FILE); 应该将 $_FILE 中的所有值都输出,但是却没有。 - BAZTED
请尝试使用var_dump($_FILES)而不是var_dump($_FILE) - Muzikant

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接