Note that there are some explanatory texts on larger screens.

plurals
  1. POSend text and image to php with java
    primarykey
    data
    text
    <p>This is my first question on stackoverflow, so if you have any comment on the way I put/explain it, I would gladly accept any pointers.</p> <p>So here it is:</p> <p>I want to sent both text and an image to a php script on a server. I can succesfully sent both apart from each other, but not together. Up till now I use this for a String:</p> <pre><code>String dataSend = URLEncoder.encode("User","UTF-8") +"="+URLEncoder.encode(username,"UTF-8"); URL url = new URL(urlCopyImage); URLConnection connection = url.openConnection(); connection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write(dataSend); out.close(); </code></pre> <p>For a image I use:</p> <pre><code>URL url = new URL(urlCopyImage); URLConnection connection = url.openConnection(); connection.setDoOutput(true); ImageIO.write(image,"PNG", connection.getOutputStream()); </code></pre> <p>Can i somehow combine these two?</p> <p>In the php script I merely use the <code>$_POST</code> to receive the string and for the image i use:</p> <pre><code>$incomingData = file_get_contents('php://input'); </code></pre> <p>Any tips are welcome. </p> <p>For the record, I use the php file to create a file containing this image on the server. The username is useful for specifying the directory.</p> <p>EDIT:</p> <p>Thanks to your answers I have been able to make it work, though I don't quite understand it yet.</p> <pre><code> MultipartEntity multipart = new MultipartEntity(); multipart.addPart("User", new StringBody(username)); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(image, "png", os); InputStream is = new ByteArrayInputStream(os.toByteArray()); ContentBody cb = new InputStreamBody(is, "something.png"); multipart.addPart("Image", cb); HttpPost post = new HttpPost(urlCopyImage); post.setEntity(multipart); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(post); </code></pre> <p>I am handing the contentbody a inputstreambody which get the bytestream from the ImageIO. That's great. But what i don't get is what the second parameter of the inputstreambody is supposed to do. Although this works, it feels not right.</p> <p>Btw, this is the php code:</p> <pre><code>$user = $_POST['User']; copy($_FILES['Image']['tmp_name'],$filename); </code></pre> <p>I would be grateful if someone could explain what exactly I'm doing here. Thanks for your help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload