POSTing .JPG from Applet

Hi,

Im trying to post a jpg from java to a php. My question is what would be the best type of stream to do this and also how do I convert my jpg (currently a BufferedImage type) to a format that can be writen to this stream?

Thanks for any pointers, Im struggling to do any more than post a string.

Tomm

[328 byte] By [spina] at [2007-10-2 15:51:16]
# 1

Really the appropriate transaction type is "PUT" rather than "POST". Cast the connection to HttpURLConnection to change the method type. A Content-length header is required (unfortunately, since it means buffering the stream you are to send).

As to format, you could use ImageIO to convert to one of the standard forms (jpeg, png etc.) or you can write binary and sort the format out for yourself.

If you use ImageIO you'll have to use a ByteArrayOutputStream to buffer the data, if only to get the length for the content-length header.

malcolmmca at 2007-7-13 15:58:38 > top of Java-index,Java Essentials,Java Programming...
# 2

ok - thanks very much for your help. Ive kinda got my brain in a twist over the order that buffered streams need to be chained together...

does the stream need to be initialised with the buffer or vice-versa? Im thinking along these lines:

BufferedImage image = ImageIO.read(new File("IMAGE TO PUT"));

JPGimage = convert_to_jpg(image);//assuming ive written this :P

String address ="WHERE THE PHP SCRIPT IS";

//open connection to address

URL dataURL = new URL(address);

URLConnection connection = dataURL.openConnection();

//set request to a POST of multipart data (eg image)

((HttpURLConnection)connection).setRequestMethod("POST");

connection.setRequestProperty("Content-Type", "multipart/form-data");

// no caching

connection.setUseCaches(false);

// allow output

connection.setDoOutput(true);

// instatiate buffered stream

ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();

// put picture in buffer

bytearrayoutputstream.write(image);

// set content-length to size of whats in buffer

String length_of_data = String.valueOf(bytearrayoutputstream.size());

connection.setRequestProperty ("Content-Length", length_of_data);

//close stream to buffer

bytearrayoutputstream.close();

//open stream to address via connection

OutputStream outputstream = connection.getOutputStream();

// write content of buffer to outputstream

outputstream.write(bytearrayoutputstream.toByteArray());

outputstream.flush();

outputstream.close();

ive found lost of examples of buffered streams but they all seam to chain things differently so its hard to get a grasp on what the common elements are.

Thanks again

Tomm

spina at 2007-7-13 15:58:38 > top of Java-index,Java Essentials,Java Programming...
# 3

java.awt.image.BufferedImage bufimg = null;

ByteArrayOutputStream baos = new ByteArrayOutputStream();

javax.imageio.ImageIO.write(bufimg, "jpg", baos);

java.net.URL url = new java.net.URL("http://hostname/xxx.php");

java.net.HttpURLConnection urlConn = (java.net.HttpURLConnection)url.openConnection();

urlConn.setRequestProperty("Content-Type","application/octet-stream");

urlConn.setRequestProperty("Content-length", "" + baos.size());

urlConn.setRequestProperty("pure-data", "yes");

urlConn.setRequestProperty("Connection","Keep-Alive");

urlConn.setDoOutput(true);

OutputStream out = urlConn.getOutputStream();

Just some ideas.

john60a at 2007-7-13 15:58:38 > top of Java-index,Java Essentials,Java Programming...