Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8839735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:18:19+00:00 2026-06-14T10:18:19+00:00

Servlet doPost handing file-uploads, InputStream in = req.getInputStream(); File file = new File(c:/8.dat); OutputStream

  • 0

Servlet doPost handing file-uploads,

    InputStream in = req.getInputStream();

    File file = new File("c:/8.dat");
    OutputStream out = new FileOutputStream(file);
    byte[] buffer = new byte[1024];

    int len =0;
    while((len=in.read(buffer))!=-1){
        out.write(buffer, 0, len);
    }
    bao.close();
    out.close();
    in.close();

Dose Request’s getInputStream Method take the http header information?

Why is the uploaded file bigger than the original?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:18:20+00:00Added an answer on June 14, 2026 at 10:18 am

    Sending files in a HTTP request is usually done using multipart/form-data encoding. This enables the server to distinguish multiple form data parts in a single request (it would otherwise not be possible to send multiple files and/or input fields along in a single request). Each part is separated by a boundary and preceeded by form data headers. The entire request body roughly look like this (taking an example form with 3 plain <input type="text"> fields with names name1, name2 and name3 which have the values value1, value2 and value3 filled):

    --SOME_BOUNDARY
    content-disposition: form-data;name="name1"
    content-type: text/plain;charset=UTF-8
    
    value1
    --SOME_BOUNDARY
    content-disposition: form-data;name="name2"
    content-type: text/plain;charset=UTF-8
    
    value2
    --SOME_BOUNDARY
    content-disposition: form-data;name="name3"
    content-type: text/plain;charset=UTF-8
    
    value3
    --SOME_BOUNDARY--
    

    With a single <input type="file"> field with the name file1 the entire request body look like this:

    --SOME_BOUNDARY
    content-disposition: form-data;name="file1";filename="some.ext"
    content-type: application/octet-stream
    
    binary file content here
    --SOME_BOUNDARY--
    

    That’s thus basically what you’re reading by request.getInputStream(). You should be parsing the binary file content out of the request body. It’s exactly that boundary and the form data header which makes your uploaded file to seem bigger (and actually also corrupted). If you’re on servlet 3.0, you should have used request.getPart() instead to get the sole file content.

    InputStream content = request.getPart("file1").getInputStream();
    // ...
    

    If you’re still on servlet 2.5 or older, then you can use among others Apache Commons FileUpload to parse it.

    See also:

    • How to upload files to server using JSP/Servlet?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using servlet 2.3 is there a way to log a user out when he
I've this servlet filter servlet called everytime a file from Restricted folder is called.
My servlet's doPost() receives an HttpServletRequest whose ServletInputStream sends me a large chunk of
I have a servlet that construct response to a media file request by reading
I've a simple little servlet, basically: void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
May 10, 2012 4:53:31 PM com.sun.xml.ws.transport.http.servlet.WSServletDelegate doPost SEVERE: caught throwable javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException - with
I know that for each request to a servlet a the doPost() or doGet()
I have the following doPost in my servlet that inserts a record into the
getting an IOExcepiton on the PrintWriter when doing this in doPost method of servlet
I have doPost in a servlet - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.