Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to post image from applet to php file?

Status
Not open for further replies.

vleesboom

Programmer
Jun 10, 2005
3
NL
I am writing an applet that can crop an image and upload it to the server.

I would like to post the image data to a .php file on the server using a form POST method.

How to do this?

I worked with HttpUrlConnections and DataOutputstreams, but I don't exactly know how to use it.

The following is the code I distilled from several posts. Yet, it is not finished:

Code:
String u = "[URL unfurl="true"]http://bla/bla.php";[/URL]
try {
  URL url = new URL(u);
  try {
    HttpURLConnection huc = (HttpURLConnection)url.openConnection();
    huc.setRequestMethod("POST");
    huc.setDoOutput(true);
    huc.setUseCaches(false);
    huc.setRequestProperty ("Content-Type", "application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]
    huc.setRequestProperty ("Content-Length",new Integer(message.length()).toString());
    DataOutputStream dos = new DataOutputStream(huc.getOutputStream());
    dos.writeBytes(WHAT TO PUT HERE???);
    dos.flush();
    dos.close();
  
    huc.disconnect();
  }
  catch(IOException ioexception) {
    ioexception.printStackTrace();
  }
}
catch(MalformedURLException malformedurlexception) {
  malformedurlexception.printStackTrace();
}

Please help. I thank you in advance.
 
Where (and what type) is you image ? You need to encode your image into a byte[].

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks for your quick reply.

The image I use is a JPEG file, that I loaded into the applet using getImage. Then I cropped the image and created the new (cropped) image using createImage.

The name of the cropped image is cropImage. I defined it elsewhere in my code.

How does that byte[] work?
And how does php know I posted a file (like the file upload form: $_FILES['cropImage']['tmp_name'];)?
 
I mean the class - what is the class name ? BufferedImage, PlanarImage, RenderedOp, RenderedImage ?

--------------------------------------------------
Free Database Connection Pooling Software
 
I haven't tried this code, but I think it should work OK.

Code:
BufferedImage bi ...

ByteArrayOutputStream baos = new ByteArrayOutputStream();
javax.imageio.ImageIO.write(bi, "jpeg", baos);

byte[] imageData = baos.toByteArray();


--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top