Link to home
Start Free TrialLog in
Avatar of hallvard
hallvard

asked on

How to extract a file from http post?

I am trying to write a java servlet that uploads a file from a client to the webserver.
The servlet should write the file to disk.
I use a html page with a form in which the user can type the filename.
The file is then posted to the servlet.

The problem is that the InputStream contains more data than the file itself.
If only small text-files are posted one could save it in a buffer, remove
the boundarys and then save it.
But I need to save _any_ file of _any_ size.

My question: Is there an easy and/or fast, way to remove the extra info?

Hallvard...
Avatar of faster
faster

The extra info is atually very useful.  The whole POST request sent by the browser is of the following structure:

Request-Line\r\n
Request Header1\r\n
Request Header2\r\n
\r\n
Request Body

The request body is the file.  To separeate the body from the rest, just search for the "\r\n\r\n".  Besides, there is one reuqest header: Content-Length that is useful to you, which will tell you the size of the body (file).

Avatar of hallvard

ASKER

The whole post does not look like you say.
The content length is the size of the post, not the body.
I'm using MSIE 4.0 p2, is it non standard?
Show me what you get.  Besides, how do you upload the file?  You mentioned to ask the user enter the filename, then how?
Ok I use the following HTML for subitting the file:

------ HTML Code Start------
<html>
<head>
<title>File upload</title>
</head>

<body>
<H2> Upload a file </H2>
      
<form  ENCTYPE="multipart/form-data" ACTION="http://dummy30.norcontrol.no:8080/servlet/UploadServlet" METHOD=POST>

<table BORDER=1 CELLPADDING=1>
<tr>
<td>File Name</td>
<td>
<input TYPE=FILE NAME="upload">
</td>
</tr>

</table>
            
<input TYPE="submit" VALUE="Upload"> <INPUT TYPE="reset" VALUE="Clear">
      
</form>
</body>
</html>
------ HTML Code End------

I use the following code for the servlet to print the file.

------ JAVA Code Start -----
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class UploadServlet extends HttpServlet {

  public void doPost(HttpServletRequest req,
                     HttpServletResponse res)
  throws ServletException, IOException {
 
    int i;
    InputStream is;
    PrintWriter pw;

    is = req.getInputStream();
    pw = new PrintWriter(res.getOutputStream());

    System.out.println("Content Length: "
    + req.getContentLength());

    while ( (i=is.read()) != -1) {
      System.out.print((char)i);
    }
    System.out.flush();
    is.close();
                                                
    res.setStatus(HttpServletResponse.SC_CREATED);
    res.setContentType("text/html");
    pw.print("<html><head><head><title>Server Response</title>");
    pw.print("<body><h1>Upload ok</h1></body></html>");
    pw.flush();
    pw.close();
  }
}
----- JAVA Code End ----

I then upload the file C:\Sign.txt containig the 3 char's: aaa
(no CRLF) using MSIE. I get the following output:

;----- Output Start -----

-----------------------------7cd28011300d4
Content-Disposition: form-data; name="upload"; filename="C:\Sign.txt"
Content-Type: text/plain

aaa

-----------------------------7cd28011300d4--
;----- Output End -----

The two dashes (--) after the last boudary does not always appear.

ASKER CERTIFIED SOLUTION
Avatar of faster
faster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial