Solved

How to extract a file from http post?

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 85
listing all functions in JavaScript 19 202
couple of eclipse 5 37
String array comparison 4 34
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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.

777 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