Go Premium for a chance to win a PS4. Enter to Win


JAVA: how to send POST variables along with multi-parft image data to php script

Posted on 2011-03-01
Medium Priority
Last Modified: 2013-11-24
i want to send post variables along with multipart data as in code below..how can i do that ?
public static void httpConn(byte[] imgData) {

        URLConnection conn = null;
        OutputStream os = null;
        InputStream is = null;

        try {
            URL url = new URL("abc.php");
            System.out.println("url:" + url);
            conn = url.openConnection();

            String postData = "";

           // InputStream imgIs = getClass().getResourceAsStream("/test.jpg");
           // byte[] imgData = new byte[imgIs.available()];
           // imgIs.read(imgData);

            String message1 = "";
            message1 += "-----------------------------4664151417711" + CrLf;
            message1 += "Content-Disposition: form-data; name=\"uploadedfile\"; info=\"xxxxxx\"; filename=\"test.jpg\"" + CrLf;
            message1 += "Content-Type: image/jpeg" + CrLf;
            message1 += CrLf;

// the image is sent between the messages in the multipart message.

            String message2 = "";
            message2 += CrLf + "-----------------------------4664151417711--" + CrLf;

            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711");
// might not need to specify the content-length when sending chunked data.
            conn.setRequestProperty("Content-Length", String.valueOf((message1.length() + message2.length() + imgData.length)));

            System.out.println("open os");
            os = conn.getOutputStream();


            int index = 0;
            int size = 1024;
            do {
                System.out.println("write:" + index);
                if ((index + size) > imgData.length) {
                    size = imgData.length - index;
                os.write(imgData, index, size);
                index += size;
            } while (index < imgData.length);
            System.out.println("written:" + index);


            System.out.println("open is");
            is = conn.getInputStream();

            char buff = 512;
            int len;
            byte[] data = new byte[buff];
            do {
                len = is.read(data);

                if (len > 0) {
                    System.out.println(new String(data, 0, len));
            } while (len > 0);


        } catch (Exception e) {
        } finally {
            System.out.println("Close connection");
            try {
            } catch (Exception e) {
            try {
            } catch (Exception e) {
            try {
            } catch (Exception e) {

Open in new window

Question by:young_buddha

Accepted Solution

iampivot earned 2000 total points
ID: 35010487
You're trying to reimplement the wheel. Look for httpclient, which lets you do this and much more with very few lines of code; http://hc.apache.org/

import org.apache.commons.httpclient.*;
// initialize the POST method
PostMethod post = new PostMethod("http://myserver/page.jsp");
post.addParameter("parameter1", "value1");
post.addParameter("parameter2", "value2");

// execute the POST
HttpClient client = new HttpClient();
int status = client.executeMethod(post);
String response = post.getResponseBodyAsString();

See eg. http://weblogs.java.net/blog/2006/11/01/quick-intro-httpclient
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 37313440
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this article, we’ll look at how to deploy ProxySQL.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question