通过HTTP Post发送图像和文本

时间:2016-12-08 22:27:10

标签: java php android http post

我有以下通过POST发送文本的WORKING代码:

public String postRequest(String mainUrl,HashMap<String,String> parameterList)
{
    String response="";
    try {
        URL url = new URL(mainUrl);

        StringBuilder postData = new StringBuilder();
        for (Map.Entry<String, String> param : parameterList.entrySet())
        {
            if (postData.length() != 0) postData.append('&');
            postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postData.append('=');
            postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        }

        byte[] postDataBytes = postData.toString().getBytes("UTF-8");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        conn.setDoOutput(true);
        conn.getOutputStream().write(postDataBytes);

        Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

        StringBuilder sb = new StringBuilder();
        for (int c; (c = in.read()) >= 0; )
            sb.append((char) c);
        response = sb.toString();
        return  response;
    }catch (Exception excep){
    }
    return response;
}

但我需要将图像与这些文本一起发送......我已经在存储上有了图像路径......

我需要在代码中进行哪些更改才能实现这一目标?

我需要将图像路径传递到HashMap并在某处(我的PHP页面将处理的那个字段)插入图像的Post Field Name,但是如何?

提前致谢!

0 个答案:

没有答案