Upload Image

Well I've created a php file which uploads images. If you access the php file directly and upload the image in the browser, the works fine. However, I'm trying to do this through a Java Applet.

I have this html form:

<form method='post' enctype='multipart/form-data'>

<input name='userfile' type='file' id='userfile' />

<input name='upload' type='submit' id='upload' value='Upload'>

</form>

And here is my Java code, which unfortunately doesn't work.

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLEncoder;

publicclass Upload{

publicstaticvoid main(String[] args){

URL url;

HttpURLConnection urlConn;

DataOutputStream printout;

try{

url =new URL("http://www.mydomain.com/upload_background.php");

urlConn = (HttpURLConnection)url.openConnection();

urlConn.setDoInput(true);

urlConn.setDoOutput(false);

urlConn.setUseCaches(false);

urlConn.setRequestMethod("POST");

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

String content ="userfile=" + URLEncoder.encode("/var/www/squadstats/statsigs/bg-sidewinder.png");

urlConn.setRequestProperty("Content-Length", content.length()+"" );

printout =new DataOutputStream( urlConn.getOutputStream() );

printout.writeBytes (content);

printout.flush ();

printout.close ();

}

catch (MalformedURLException me)

{

System.err.println("MalformedURLException; " + me);

}

catch (IOException ioe)

{

System.err.println("IOException; " + ioe.getMessage());

}

}

}

Please help.

Thanks

[3246 byte] By [SidewinderXa] at [2007-11-27 11:18:45]
# 1

> urlConn.setDoOutput(false);

?

hiwaa at 2007-7-29 14:32:50 > top of Java-index,Java Essentials,Java Programming...
# 2

Setting it to true doesn't work either :[

Any other ideas?

SidewinderXa at 2007-7-29 14:32:50 > top of Java-index,Java Essentials,Java Programming...
# 3

> Any other ideas?

I would have started by finding out what "doesn't work" means. You would be the best one to tell us that.

DrClapa at 2007-7-29 14:32:50 > top of Java-index,Java Essentials,Java Programming...
# 4

The image does not upload - No errors are thrown - The application executes to completion [a simple system.out.println("finished") on the very last line is executed].

Eclipse is telling me URLEncoder.encode is depreciated, but the method should still work. If I had to guess where my problem is, its probably somewhere after setRequestProperty and before printout.flush.

More than likely, the code above is working, but its not posting the correct information to the php file. It's probably something with content - like it not being the right type, or encoding, or right value...but im not guru so idk...

SidewinderXa at 2007-7-29 14:32:50 > top of Java-index,Java Essentials,Java Programming...
# 5

Maybe I'm being dense, but I don't see where you upload the file. I see code that uploads a file name, but nothing that uploads the contents of the file.

DrClapa at 2007-7-29 14:32:50 > top of Java-index,Java Essentials,Java Programming...
# 6

The php file uploads the image, it just needs the file posted to it. At least I think thats how it is suppose to work.

SidewinderXa at 2007-7-29 14:32:50 > top of Java-index,Java Essentials,Java Programming...
# 7

> The php file uploads the image, it just needs the

> file posted to it. At least I think thats how it is

> suppose to work.

No.

The PHP handles the uploaded file once it has been posted by the browser or in this case your Java program. It does not work automagically, you have to post the actual content.

You aren't doing that.

cotton.ma at 2007-7-29 14:32:50 > top of Java-index,Java Essentials,Java Programming...
# 8

How do I do that? By creating a new File() and posting that rather than posting the string path?

SidewinderXa at 2007-7-29 14:32:50 > top of Java-index,Java Essentials,Java Programming...
# 9

No. You have to upload the contents of the file. So the file name is of no use, and neither is a File object referring to the file. You need to copy the contents of the file to the request.

But I think you also need to send the file name as a parameter, so I don't exactly know what else you need to send besides the content and how. Looking at what a browser sends might be a good idea. I use Firefox and Live HTTP Headers for that sort of thing.

DrClapa at 2007-7-29 14:32:50 > top of Java-index,Java Essentials,Java Programming...