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

Discussion in 'Java' started by Roland Poellinger, Mar 11, 2005.

  1. 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();
    }
    }
    ===============================================
     
    Roland Poellinger, Mar 11, 2005
    #1
    1. Advertising

  2. Roland Poellinger

    Ryan Dillon Guest

    Ryan Dillon, Mar 12, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jorch
    Replies:
    2
    Views:
    1,271
    Jorch
    May 18, 2004
  2. Replies:
    3
    Views:
    864
    Oliver Wong
    Dec 5, 2006
  3. RichB
    Replies:
    4
    Views:
    1,122
    Steven Cheng
    Mar 20, 2008
  4. Rajive Narain
    Replies:
    0
    Views:
    1,989
    Rajive Narain
    Sep 18, 2009
  5. Dan
    Replies:
    3
    Views:
    135
Loading...

Share This Page