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

Posted on 2011-03-01
Last Modified: 2013-11-24
i want to send post variables along with multipart data as in code 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()];

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

                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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

iampivot earned 500 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;

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.
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

756 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