?
Solved

How to extract a file from http post?

Posted on 1997-08-20
5
Medium Priority
?
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
A solution for Fortify Path Manipulation.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month11 days, 6 hours left to enroll

770 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