[Applet] Image-Upload to Server via Applet+PHP

R

Roland Poellinger

hello!

i know that this is a question that comes up very often - but i have never
found a complete working modifiable example, therefore i am posting this
question here again:

right now i am working on an online paint-program as java applet (where you
can choose pencil shape, choose color, hit one single button "save"). the
"save" button should send the java intern format Image or BufferedImage over
to a php-script as jpeg/gif/png. the php-script is the same that i am using
for image upload from a html/php-form in my cms (the picture has a fixed
destination - it is always the same location - simply overwriting the old
picture). the applet and the script are in the same folder on the same
server.
maybe somebody knows a configurable source example!?
i enclosed a bit of code from another forum about which somebody might be
able to tell me what would have to be modified to be able to send
Image/BufferedImage to php.
thankfully -
roland

EXAMPLE FOR TEXT FROM APPLET TO PHP:
===================================

import java.net.*;
import java.io.*;
public class srvHead implements Runnable{
String c = "";


public srvHead(){
new Thread(this).start();
}
public void run(){
try{
// run this as an application first, when it works than make an applet of it
URL u = new URL("http://yourserver/yourpage.php");
URLConnection c = u.openConnection();
// post multipart data
c.setDoOutput(true);
c.setDoInput(true);
c.setUseCaches(false);
// set some request headers
c.setRequestProperty("Connection", "Keep-Alive");
// TODO: get codebase of the this (the applet) to use for referer
c.setRequestProperty("HTTP_REFERER", "http://applet.getcodebase");
c.setRequestProperty("Content-Type", "multipart/form-data;
boundary=****4353");
DataOutputStream dstream = new DataOutputStream(c.getOutputStream());
// TODO: next line sends form data like a text input, to send binary data I
have too look in to the request protocol for multipart form data
// TODO: when sending a file a FileInputStream needs to be opened
(Jfilechooser ?)
dstream.writeBytes("--****4353\r\nContent-Disposition: form-data;
name=\"uniqueURL\"\r\n\r\n1086774178716\r\n--****4353--\r\n\r\n");
dstream.flush();
dstream.close();
// reading all the header fields of the response
try{
int i = c.getHeaderFields().size()-1;
while(i>-1){
try{
System.out.print(c.getHeaderFieldKey(i));
System.out.print("=");
System.out.println(c.getHeaderField(i));
}catch(Exception e){
}
i--;
}
}catch(Exception e){
}
try{
DataInputStream in =
new DataInputStream(
new BufferedInputStream(c.getInputStream()));
String sIn = in.readLine();
boolean b = true;
// TODO: this will loop forever unless you make sure your server page
// sends a last line like "I am done"
// than you can do wile(sIn.compareTo("I am done")!=0){
while(b){
if(sIn!=null){
System.out.println(sIn);
}
sIn = in.readLine();
}
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new srvHead();
}
}
===============================================
 

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,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top