Upload from Applet to Servlet

Discussion in 'Java' started by =?ISO-8859-1?Q?Sascha_M=F6llering?=, Nov 15, 2003.

  1. Hi,

    I want to upload a file (as a byte-array) and several parameters
    (filename etc.) from an applet to a servlet. I tried using the
    HttpMessage-Class from the COS-Classes, but that does not work (Missing
    boundary ..) as I expected.
    Does anybody have working sample code?


    Thank you in advance,

    Sascha
     
    =?ISO-8859-1?Q?Sascha_M=F6llering?=, Nov 15, 2003
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Sascha_M=F6llering?=

    Ike Guest

    Use FileOutputStreams to send arrays of serializable Objects to
    HTTPServlet.service(). Here is a pared down version of how I do it:

    public class YourServlet extends HttpServlet{
    public void service(HttpServletRequest request, HttpServletResponse
    response) throws ServletException, IOException {
    try {

    ObjectInputStream inputFromApplet = new
    ObjectInputStream(request.getInputStream());
    Object connArray[] = (Object[])inputFromApplet.readObject();
    inputFromApplet.close();

    //now, in my servlet, if I am, for example, looking for a string
    in element 3 of the array of objects passed:
    (String)connArray[3]
    //just cast to whatever it is when sent:(sometimes, I use
    element 0 as an ArrayList of the types that follow!
    ((ArrayList)connArray[0])
    (Double)connArray
    // see?

    //now, you can even send things back to the applet this way
    // send objects back to applet
    ObjectOutputStream outputToApplet = new
    ObjectOutputStream(response.getOutputStream());
    outputToApplet.writeObject(/*some serializable object such as
    Object[] as we sent to the servlet*/);
    outputToApplet.flush();
    outputToApplet.close();
    }catch(Exception e){}
    }
    }

    //and in your applet, call the servlet and pass it objects:

    try {
    String host = applet.getDocumentBase().getHost();
    URL servletURL = null;
    String servletURLstring;// eg "/servlets/YourServlet"
    servletURL = new URL("http://" + host + servletURLstring);
    System.out.println("servlet invoked:" + servletURL);
    URLConnection uc = servletURL.openConnection();
    uc.setDoOutput(true);
    uc.setDoInput(true);
    uc.setUseCaches(false);
    uc.setRequestProperty("Content-type","application/octet-stream");

    ObjectOutputStream objOut = new
    ObjectOutputStream(uc.getOutputStream());
    int sz=5; //telling it the size in advance

    Object conArray[] = new Object[sz];
    conArray[0] = //whatever objects I
    conArray[1] = //wish to send...

    objOut.writeObject(conArray);
    objOut.flush();
    objOut.close();

    // get objects from servlet
    ObjectInputStream objIn = new ObjectInputStream (uc.getInputStream());
    String returnString = (String)objIn.readObject();
    objIn.close();

    catch (Exception e) {
    e.printStackTrace();
    }
    //Good luck, pls advise that you got this to work. -Ike

    "Sascha Möllering" <> wrote in message
    news:bp52eu$drk$08$-online.com...
    > Hi,
    >
    > I want to upload a file (as a byte-array) and several parameters
    > (filename etc.) from an applet to a servlet. I tried using the
    > HttpMessage-Class from the COS-Classes, but that does not work (Missing
    > boundary ..) as I expected.
    > Does anybody have working sample code?
    >
    >
    > Thank you in advance,
    >
    > Sascha
    >
     
    Ike, Nov 15, 2003
    #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. Andy Fish
    Replies:
    4
    Views:
    2,453
    Andy Fish
    Dec 17, 2003
  2. circuit_breaker
    Replies:
    2
    Views:
    2,104
    Jack Jia
    Apr 4, 2004
  3. bugbear
    Replies:
    0
    Views:
    507
    bugbear
    Oct 7, 2004
  4. Roland Poellinger
    Replies:
    1
    Views:
    1,345
    Ryan Dillon
    Mar 12, 2005
  5. Tedy
    Replies:
    1
    Views:
    473
    Andrew Thompson
    Aug 4, 2005
Loading...

Share This Page