4

I have the following WORKING code that sends text via POST:

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

But I need to send an image together with those texts... I already have the image path on the storage...

What do I need to change in my code to achieve that??

I need to pass the image path into the HashMap and insert the Post Field Name of the image somewhere (that field that my PHP page will handle), but how?

Thanks in advance!

T. Lima
  • 238
  • 3
  • 13
  • 1
    are you asking how to send an image file to your server? – BiGGZ Dec 08 '16 at 22:35
  • exact! that's what I'm looking for... – T. Lima Dec 08 '16 at 22:37
  • do you want to send image binary data alongside some text values like a multipart/form or just want to send a link of image? – uylmz Dec 08 '16 at 22:42
  • Expanding on @Reek's comment, using [this answer](http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean/28380690#28380690) can help build an HTTP POST packet to send an image and text data. They use an octet-stream in the example, but you could even base64 encode images if you wanted. – Froopy Dec 08 '16 at 22:48
  • @Reek I want to send the file somehow, using that multipart/form... Not just a link – T. Lima Dec 08 '16 at 22:50
  • @Froopy Thank you so much, I'm gonna take a look... But I don't know what for base64 encoding is used, I don't think I will need that... – T. Lima Dec 08 '16 at 22:51
  • found a tutorial descripbing multipart/form-data https://www.techcoil.com/blog/how-to-upload-a-file-via-a-http-multipart-request-in-java-without-using-any-external-libraries/ posting some text and a file, using pure java. It seems easier using apache's HttpUtils though. – uylmz Dec 08 '16 at 22:56
  • @T.Lima, if you want to transfer an image, you will need to convert it to a Base64(or 91) string – BiGGZ Dec 08 '16 at 22:56
  • i recommend you use this library square.github.io/okhttp/ for your IO, a lot less boilerplate code – BiGGZ Dec 08 '16 at 22:57
  • check this answer out for Base64'ing an image: http://stackoverflow.com/questions/7360403/base-64-encode-and-decode-example-code – BiGGZ Dec 08 '16 at 22:59
  • @T.Lima Base64 encoding is used to encode and transmit binary files such as image, pdf, application and other binary files. But base64 data is quite heavier like 1.333 times than the original data. – Wolverine Dec 08 '16 at 23:06
  • @BiGGZ, you don't need to base encode an image to send it via post. That's just another option. If you set `Content-Transfer-Encoding: binary` you can just feed the direct bytes into the POST request like [here](http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically) – Froopy Dec 08 '16 at 23:12
  • The server decides how the image has to be sent. So first tell us what the server expects. You just can not start sending if you dont know. – greenapps Dec 08 '16 at 23:30
  • @greenapps Of course... my PHP server gets like that: $imagem=($_FILES['photo']['name']); and then moves the image to a directory: move_uploaded_file($_FILES['photo']['tmp_name'], $imagem)... just that – T. Lima Dec 08 '16 at 23:43
  • Well put that at the start of your post. Not in a comment. Start your story with it as it is pretty important information. Put it in a code block. – greenapps Dec 09 '16 at 08:36

0 Answers0