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

x
?
Solved

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

Posted on 2011-03-01
3
Medium Priority
?
347 Views
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();
            conn.setDoOutput(true);

            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();

            System.out.println(message1);
            os.write(message1.getBytes());

// SEND THE IMAGE
            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(message2);
            os.write(message2.getBytes());
            os.flush();

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

            char buff = 512;
            int len;
            byte[] data = new byte[buff];
            do {
                System.out.println("READ");
                len = is.read(data);

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

            System.out.println("DONE");

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

Open in new window

0
Comment
Question by:young_buddha
3 Comments
 
LVL 1

Accepted Solution

by:
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
0
 
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.
0

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