Send file upload request from applet to php script on server
843807Dec 18 2005 — edited Feb 6 2006I'm just starting out playing around with applets, but have already got into areas well beyond my expertise...
Basically, I have an unsigned applet up on a server, that generates an Image object, and I want to save this image to a file on my server.
I've got the Image object set up, as well as a URLConnection,
and then on the other side, I've got my php script ready to accept and write a jpeg file that it's expecting to receive via an html form upload.
What I need is the inbetween, where the applet generates the html headers for the post request, and sets up the image so it can be transferred as well.
I don't know what to do with the Image object, because I can't very well write it to a file first (or else this would be sooo much easier), and I don't quite understand how it's even stored at the moment (ie. how to convert properly for the transfer), maybe it should be sent as a byte[]?
Anyhow, here's a little bit of code that might illustrate better.
------------------------
outImage = createImage(getSize().width, getSize().height);
Graphics g = outImage.getGraphics();
g.drawImage(offScreenImage, 0, 0, null);
//outImage is ready to go
URL urls = new URL("http","www.yourdomain.com/imageupload.php");
URLConnection con = urls.openConnection();
///missing code :)////
-----------------
Want data to appear as form:
<form method="post" action="imageupload.php" enctype="multipart/form-data" >
<input type="file" name="image">
</form>
-----------------------------
----->php script
<?php
/* Where the file is going to be placed */
$target_path = "uploads/";
/* Add the original filename to our target path. Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['image']['name']);
if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['image']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
-------------------------
ALSO,
Will the following send the image as a file?
And then I don't know how to get the file length from this (can't be as simple as "Content-length", outImage.length()+"", can it?)
try {
ImageIO.write(outImage, "jpeg", OutputStream);
}
catch (Exception e) {
}
-----------------------------------
If you can help me out I would be much obliged, because I've scoured around the net and found a gazillion other people trying to write images to a server from an applet, with no apparent success.....so maybe this can help a lot of people?
;)