Solved

How to extract a file from http post?

Posted on 1997-08-20
5
274 Views
Last Modified: 2013-11-24
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...
0
Comment
Question by:hallvard
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:faster
ID: 1225450
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).

0
 

Author Comment

by:hallvard
ID: 1225451
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?
0
 
LVL 7

Expert Comment

by:faster
ID: 1225452
Show me what you get.  Besides, how do you upload the file?  You mentioned to ask the user enter the filename, then how?
0
 

Author Comment

by:hallvard
ID: 1225453
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.

0
 
LVL 7

Accepted Solution

by:
faster earned 100 total points
ID: 1225454
What I gave you is the HTTP protocal, i.e. the data that will be transferred to the server.  Unfortuately, the server seems to have done some processing before it pass the data to you.  But after the "----------------------------7cd28011300d4 ", you still get something similiar: request header and body.  Are you able to get the number "7cd28011300d4"?  If so, there should be no problem extracting the body.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse apache tomcat admin console 52 153
runtime exception 2 50
how to debug htl and js pages 8 56
netbeans can not run self signed app 8 12
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question