Solved

How to extract a file from http post?

Posted on 1997-08-20
5
265 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now