images, writing over url connection

N

news.skynet.be

Hello, pretty new to java so bear with me.

I am trying to write an image over an HttpURLConnection.

I have an example of how to write text but I am having real problems trying
to write an image, just can't get my head around it.

I have succeeded in writing to the local HD using ImageIO:
//start image output
FileOutputStream fo = new FileOutputStream("myImg.jpg");
BufferedOutputStream bo = new BufferedOutputStream(fo);
ImageIO.write(destination, "jpeg", bo);
bo.close();
//end image output

This works a treat. I understand how to set up an url connection and then
write text to it:

//start text to url
URL url = new URL( "http://www.myurl.com/myfolder/aFile.cfm" );
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection( );

urlcon.setRequestMethod("POST");
urlcon.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
//image/jpeg

urlcon.setDoOutput(true);
urlcon.setDoInput(true);

PrintWriter pout = new PrintWriter( new
OutputStreamWriter(urlcon.getOutputStream( ), "8859_1"), true );

pout.print( formData );//form data collected from app
pout.flush( );
// end text to url

What I am having a problem with is this bit:
PrintWriter pout = new PrintWriter( new
OutputStreamWriter(urlcon.getOutputStream( ), "8859_1"), true );

Where I want to put in my buffered output stream, and then use that with
ImageIO, I just can't see where I am supposed to start.... or have I missed
the boat entirely

Thanks for taking the time to read this post.
cheers
Martin

Thanks for your time
 
M

Michael Borgwardt

news.skynet.be said:
What I am having a problem with is this bit:
PrintWriter pout = new PrintWriter( new
OutputStreamWriter(urlcon.getOutputStream( ), "8859_1"), true );

Where I want to put in my buffered output stream, and then use that with
ImageIO, I just can't see where I am supposed to start.... or have I missed
the boat entirely

You have. PrintWriter and OutputStreamWriter are for writing text data.
Just use the result of urlcon.getOutputStream() directly with ImageIO,
maybe buffer it first.
 
N

news.skynet.be

Thanks for taking the time to read... ok if I am reading you correctly do
you mean something like this:

//connection to the server
URL url = new URL( "http://127.0.0.1/testing/javaUpload/myImg.jpg" );
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection( );
urlcon.setRequestMethod("POST");
//file type is image
urlcon.setRequestProperty("Content-type", "image/jpeg");
urlcon.setDoOutput(true);
// urlcon.setDoInput(true);
//now just need to write my image to the output stream
ImageIO.write(destination, "jpeg", urlcon.getOutputStream());

My worry with this is I am not actually creating a file, and uploading that
file, just calling an url of where a file does not exist.. so I think I am
missing something there... but I looked at the URL documentation and it
states that you can specify a protocol, host and file, so I thought I might
be able to do some new File stuff in the third property... although my
Eclipse did not like it at all...

Any suggestions,, sorry to hassle but I am pretty new at this java stuff..
it is a large world to get to grips with.

thanks for reading

cheers
Martin
 
M

Michael Borgwardt

news.skynet.be said:
Thanks for taking the time to read... ok if I am reading you correctly do
you mean something like this:

//connection to the server
URL url = new URL( "http://127.0.0.1/testing/javaUpload/myImg.jpg" );
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection( );
urlcon.setRequestMethod("POST");
//file type is image
urlcon.setRequestProperty("Content-type", "image/jpeg");
urlcon.setDoOutput(true);
// urlcon.setDoInput(true);
//now just need to write my image to the output stream
ImageIO.write(destination, "jpeg", urlcon.getOutputStream());

My worry with this is I am not actually creating a file, and uploading that
file, just calling an url of where a file does not exist.. so I think I am
missing something there...

Yes, there's definitely something missing there, but mostly not in your code.
You need cooperation from the *server* to create a file there. Just writing
to an URL is not possible (it would be a horrible security hole). There's two
ways to do it: via the HTTP POST method or the PUT method. The first requires
some sort of CGI script or servlet to take that data and create the file,
and it means more complicated code for you because you can't just write the
file, you need to encode it. The Apache commons HTTP client makes this easier.
I think with the PUT method, you can indeed just send the file, but it requires
special configuration of the webserver.
 
Y

Yu SONG

news.skynet.be said:
Thanks for taking the time to read... ok if I am reading you correctly do
you mean something like this:

//connection to the server
URL url = new URL( "http://127.0.0.1/testing/javaUpload/myImg.jpg" );
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection( );
urlcon.setRequestMethod("POST");
//file type is image
urlcon.setRequestProperty("Content-type", "image/jpeg");
urlcon.setDoOutput(true);
// urlcon.setDoInput(true);
//now just need to write my image to the output stream
ImageIO.write(destination, "jpeg", urlcon.getOutputStream());

My worry with this is I am not actually creating a file, and uploading that
file, just calling an url of where a file does not exist.. so I think I am
missing something there... but I looked at the URL documentation and it
states that you can specify a protocol, host and file, so I thought I might
be able to do some new File stuff in the third property... although my
Eclipse did not like it at all...

Any suggestions,, sorry to hassle but I am pretty new at this java stuff..
it is a large world to get to grips with.

thanks for reading

cheers
Martin

You have just finished a client part

To answer your questions,
It depends how that server behaves, whether the server allows you to
post( & create) a file or not.

You may add some variables in your post data, which could be helpful for
your server-side scripts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top