Results 1 to 7 of 7
  1. #1
    hofsoc is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default Upload file to servlet and pass file to another servlet

    I want to upload a file to a servlet from .net application. File is uploading to the first servlet using WebClient.UploadFile on client side and Commons FileUpload on server side. I need to pass the file content to a second servlet which will then upload the file contents to a server. I have converted the bytes within the first servlet to string but not sure how to go about it from here... file content type needs to be multipart/form-based so do i need to create a file object and pass this to the second servlet...??

    All suggestions welcome...

    This is my server side code thus far:

    boolean isMultipart = ServletFileUpload.isMultipartContent(request);

    if(isMultipart){

    _log.debug("Content is multipart.");

    //Create a factory for disk-based file items
    FileItemFactory factory = new DiskFileItemFactory();

    //Create a new File upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);

    try {
    List items = upload.parseRequest(request);

    //Process the uploaded items
    Iterator iter = items.iterator();

    while(iter.hasNext()){

    FileItem item = (FileItem) iter.next();

    // Process the file upload

    byte[] data = item.get();

    String str = new String(data);


    } catch (FileUploadException e) {
    e.printStackTrace();
    }

    Thanks in advance...
    Last edited by hofsoc; 06-03-2009 at 08:54 PM.

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    Strings don't enter into it. Your file data is bunch of bytes. Keep it as a bunch of bytes.

    From your first servlet, just treat the second as a boring old HTTP connection from a URL. Off t'top of t'head, something like this:

    Java Code:
            URL url = new URL("http://path.to.my/servlet/");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            OutputStream out = new BufferedOutputStream(conn.getOutputStream());
            // write bytes to out
    Then, from your second servlet, call getInputStream on the request object and read the data in.

    Of course:
    - if the two servlets are running in the same JVM, they can just "see" each other directly-- just use a standard producer-consumer model for one servlet to push the data to the other;
    - if your network environment permits it, server 2 is free to open any old listening socket, and for server 1 to connect to bypass HTTP altogther and connect directly to that socket.
    Last edited by neilcoffey; 06-04-2009 at 05:27 PM. Reason: added setDoOutput()

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Sounds like ASP.NET and a Java WAS. What Neil suggested will work if the two servers are allowed to communicate directly. If that is not the case, you can take the content you uploaded and run it through an HTML-safe encoding process (CODE64?) and put the content inside a text field in a form in a small HTML page. Use Javascript to immediately post the form to the second server (this avoids redirect issues at the browser).

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    Sorry, read too quickly and missed the .NET bit -- so is the "second Servlet" not actually a servlet, then? But either way, presumably .NET has a way to just "pull down the bytes" that are posted to it, no?

  5. #5
    hofsoc is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    The second servlet is a servlet - in the first servlet I will be completing some validation on the file contents (hence the reason I am converting to string). Once the validation passes I will convert string back to bytes and stream this on to the second servlet which will upload to a server. The response from the first servlet will be returned in byte format to the .NET app which is fine!

  6. #6
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    OK, then I think you can just do what I mentioned. N.B. I corrected my above code slightly-- I forgot the call to setDoOutput(true).

    Be very very careful of conveting to a String and then back again. If I got a dollar for every time somebody posted about a problem that occurred because they'd done this, I'd have at least enough for a glass of Merlot. By all means convert to a string for the purposes of validation, but keep on to the original bytes and send those, unless you're super-sure that that's not what you need to do, and you fully understand character conversion etc.

  7. #7
    hofsoc is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    Great thanks - good to know about converting back and forth between string and bytes... ;)

Similar Threads

  1. How to pass value from servlet to midlet?
    By sharmaj2me in forum CLDC and MIDP
    Replies: 1
    Last Post: 12-05-2008, 04:52 PM
  2. how to pass a List from a servlet to javascript..
    By hussainzim in forum Java Servlet
    Replies: 2
    Last Post: 09-09-2008, 01:17 PM
  3. Replies: 0
    Last Post: 08-30-2008, 12:52 PM
  4. File Upload Servlet problem
    By jeniramires in forum Advanced Java
    Replies: 3
    Last Post: 08-18-2008, 08:34 PM
  5. how to upload a file using servlet
    By simon in forum Java Servlet
    Replies: 1
    Last Post: 08-05-2007, 04:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •