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 7001899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:51:41+00:00 2026-05-27T20:51:41+00:00

I have a file uploader on my website, since i cannot use php I’m

  • 0

I have a file uploader on my website, since i cannot use php I’m using jsp pages.
My main page uses a hidden iframe to post data to a second jsp page that handles the upload. However the uploaded images is always corrupted, more specifically it’s larger in size than the original file.
Any hints or tips would be appreciated.
Main page code:

<form id="uploadForm">
    <input type="file" name="datafile" /></br>
    <input type="button" value="upload" onClick="fileUpload(document.getElementById('uploadForm'),'single_upload_page.jsp','upload'); return false;" >
</form>

The code for fileUpload concerning the form:

form.setAttribute("target","upload_iframe");
form.setAttribute("action", action_url);
form.setAttribute("method","post");
form.setAttribute("enctype","multipart/form-data");
form.setAttribute("encoding","multipart/form-data");

// Submit the form...
form.submit();

The code that handles the upload:

DataInputStream in = new DataInputStream(request.getInputStream());
int dataLength = request.getContentLength();

Because of the varying size of dataLength I’m assuming that the request.getInputStream receives extra data.

I only posted the code I think matters, if I need to post more or if you need any more information don’t hesitate to ask.

  • 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-05-27T20:51:41+00:00Added an answer on May 27, 2026 at 8:51 pm

    Simple request

    The problem is request.getContentLength() gives the length of the whole request, containing headers and all.

    You have to search for Content-Length header value, convert it to Long and that’s the proper size.

    if you can’t get it (it can be unavailable) simply consume the whole input stream. But of course, you’ll not have idea of the file size.

    Multipart request

    Anyway… as your form is multipart/form-data you should use some library to parse all the different parts, find the one you need (the file part) and read it. You can use commons-fileupload.

    Sample from real life

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp)
            throws ServletException, IOException 
    {
            // (....)
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload sfu = new ServletFileUpload(factory);
            FileItemIterator it = sfu.getItemIterator(request);
            // TAKE THE FIRST PART FROM REQUEST (HERE COMES THE FILE)
            if (it.hasNext())
            {
                FileItemStream fis = it.next();
                // grab data from fis (content type, name)
                ...fis.getContentType()...
                ...fis.getName()...
                // GET CONTENT LENGTH SEARCH FOR THE LENGTH HEADER
                ...getContentLength(fis.getHeaders(), request)...
                // here I use an own method to process data
                // but FileItemStream has an openStream method
                FileItem item = processUpload(factory, fis, uploadInfo);
                (....)
            }
    
    
    
    private long getContentLength(FileItemHeaders pHeaders, HttpServletRequest request)
    {
        try
        {
            return Long.parseLong(pHeaders.getHeader("Content-length"));
        }
        catch (Exception e)
        {
                        // if I can't grab the value return an approximate (in my case I don't care)
            return request.getContentLength();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP file for my website, index.php, which, as it stands on
I have read the following tutorial Uploading Files To the Server Using PHP and
Can someone explain to me why this happens? I have a file called profile.php
Possible Duplicate: HTML Compress File Upload? I have complains from client that my website
I have form for file uploading in my website that i check mime-type of
I am trying to upload a file of 1GB size using php script and
On the PHP website, the only real checking they suggest is using is_uploaded_file() or
I have this file uploader inside an iframe, but when I embed it to
Here's my conundrum: I have a page that uses Google Maps V3 and jQuery.
I'm trying to add a file upload script in php to a website I'm

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.