如何在android中使用httppost在服务器上发送imagedata?

时间:2015-03-20 21:22:12

标签: android http-post

我尝试将图像数据转换为“String str = Base64.encodeToString(imagedata,Base64.DEFAULT);”然后我试着发送但它不起作用。

即使我在下面试过,

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setCharset(MIME.UTF8_CHARSET);

        builder.addBinaryBody("file",imagedata);

        httpPost.setEntity(builder.build()); 

仍然无法发送图片。

我尝试发送的服务器网址如下,

http://192.168.1.8:88/erp/demo.nsf/(demo)?operation&file=imagedata

请给我一些建议。

谢谢。

1 个答案:

答案 0 :(得分:0)

我不认为你的网址应该带参数。您使用的任何参数都必须作为文本添加到构建器中。这是我最近用来做同样事情的一些代码。:

@Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        String textFileName = params[0];
        String message = "This is a multipart post";
        String result =" ";
        HttpPost post = new HttpPost("http://10.0.2.2/test/upload_file_test.php");
        File file = new File(textFileName);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addBinaryBody("uploaded_file", file, ContentType.DEFAULT_BINARY, textFileName);
        builder.addTextBody("text", message, ContentType.DEFAULT_BINARY);

        HttpEntity entity = builder.build();
        post.setEntity(entity);
        HttpClient client = new DefaultHttpClient();
        try {
            HttpResponse response = client.execute(post);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();

            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
            System.out.println("Response: " + s);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return message;
    }

}

我使用文本部分发送带有图像的消息。