*
The moose likes Servlets and the fly likes How send file data to JSP/Servlet page and save to disk? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How send file data to JSP/Servlet page and save to disk?" Watch "How send file data to JSP/Servlet page and save to disk?" New topic
Author

How send file data to JSP/Servlet page and save to disk?

Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
I have an application that will open a socket to a web page (A Servlet or JSP), get the bytes of a file and send that file data (along with file name, username and password) to the servlet/jsp. I want the server-side code to then take that data and save it to disk as that filename. How would I do this?
I'm figuring I'd use either POST or PUT on the client-side for the command sent to the server? How do I set up a servlet to handle a PUT request? Can i send other info with a PUT request?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Have you considered a jsp/servlet that responds to multi-part requests (with say a package like com.oreilly.servlet)?

Get this part working with a plain HTML page that POSTS to the servlet, the filename, username and password in plain text fields, and the file data in a file upload field.

Then use a packet sniffer to determine the format of a multi-part POST and use this info to construct your own POST from the application over the socket.
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
I believe I get what you're saying. You just mean, send all the commands that a browser would for a POST, right? So simulate a browser post with my socket app. Am I reading that correctly?
That sounds fairly easy if that's what you're thinking, I just need to know the commands (The socket is in VB, the web server is tomcat)
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

That's what I meant, yes.

I'd sniff the packets as they came through and simply construct a byte stream, or the VB equivalent that mimics the format of a multi-part post.

I did it once, and posted the results here
[ March 21, 2003: Message edited by: Mike Curwen ]
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
Thanks for the reply. I know what the posted info looks like on the server side (I've written a parser for that before), but what does the client-side code command look like? That I don't know because JSP doesn't let you see it (that I know of).
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I'm not sure what you mean by "code command".

Unless I'm gravely mistaken, what you get on the server-side is exactly what was sent from the client. You would construct a sequence of characters that exactly duplicates the sequence of characters that you can sniff on the server-side.

Send that sequence of characters through a socket that connects to the correct IP and port, and it should be understood on the other end as "aha, an HTTP request".

Whatever is listening to port 80 is expecting HTTP requests. And as long as the packets that it receives can be decoded to look like "POST http://205.200.230.44:8080/ee/edit HTTP/1.0\n", etc, etc... then that is the 'command'.

If you mean the command to send this sequence of characters, I don't know.. you're doing it in VB right?

Hopefully Bill Brogden will notice this thread and butt in if I'm wrong (or anyone else).
[ March 21, 2003: Message edited by: Mike Curwen ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Mike - if I understand whats required, you are on the right track. (Of course if he wanted a Java application he could use the HttpURLConnection.)
Bill
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
No, you're right on. It's weird but my browser wasn't showing your code in full. I didn't see the headers. Thanks for posting it!
But my one question: how do I generate a proper boundary (delimiter)?
[ March 21, 2003: Message edited by: Robert Paris ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

In the other post that I linked to ( this post ), the 4th post to that thread is me showing how to use \r\n, and it also shows you what a boundary looks like.
[ March 22, 2003: Message edited by: Mike Curwen ]
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
Right, but I mean how do I generate the boundary. I mean I could just reuse the boundary you provided, but I'd prefer to generate one each time so they're unique (in case there's a need some time for uniqueness)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How send file data to JSP/Servlet page and save to disk?