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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:49:13+00:00 2026-06-11T19:49:13+00:00

I have a JSP page and a servlet.The JSP has two input, one being

  • 0

I have a JSP page and a servlet.The JSP has two input, one being the File and the other is a text input.When user clicks on the SUBMIT button the Form action points to Servlet`s do Post().In Do Post() I have didvided the code into two part.The one part retrives the text input and File name from Jsp page and the other converts the file into bytes.

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
                                                                                    IOException {
    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();

    //This is 1st part
    //For Converting the File into Stream of Bytes
    String contentType = request.getContentType();
    //System.out.println("Content type is :: " +contentType);

    if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
        DataInputStream in = new DataInputStream(request.getInputStream());
        int formDataLength = request.getContentLength();
        byte dataBytes[] = new byte[formDataLength];
        int byteRead = 0;
        int totalBytesRead = 0;
        int k = -1;
        while (totalBytesRead < formDataLength) {
            byteRead = in.read(dataBytes, totalBytesRead, formDataLength);

            totalBytesRead += byteRead;
            //System.out.println(dataBytes[++k]);

        }
        for (int i = 0; i < formDataLength; i++) {
            System.out.print((char)dataBytes[i]);

        }

        System.out.println("Converted");
        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("</HEAD>");
        out.println("<BODY>");
        out.println("<H1>UPLOADED FILE</H1>");
        out.println("<BODY>");
        out.println("</HTML>");
    } else
        System.out.println("asa");


    //This is the 2nd part
    // Create a new file upload handler
    DiskFileUpload upload = new DiskFileUpload();

    // parse request
    List items = null;

    // get uploaded file
    try {
        items = upload.parseRequest(request);
    } catch (FileUploadException e) {
    }
    FileItem file = (FileItem)items.GET(1);
    String DocTitle = file.getName();

    int len = DocTitle.length(), pos = 0, j = 2;
    for (int i = 0; i < len; i++) {

        if (DocTitle.charAt(i) == 46) {
            pos = i;

        }

    }

    String s = DocTitle.substring(pos + 1, len);
    System.out.println("TheContent Type is: " + s);

    // get taget filename
    FileItem name = (FileItem)items.get(1);
    String fileName = name.getString();
    System.out.println("Filename: " + fileName + "." + s);

}

The problem is if i only run only the 1st part or 2nd part the code works but together they dont seem to work.If both are put together then the 1st part get executed and for 2nd part it throws

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)

Please help me to solve the above

  • 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-11T19:49:14+00:00Added an answer on June 11, 2026 at 7:49 pm

    Your concrete mistake is that you’re attempting to read the HTTP request body twice. The first time using request.getInputStream() with apparently the sole purpose to print it to the stdout (perhaps a careless debug attempt?). The second time using Apache Commons FileUpload. However, it would retrieve a completely empty request body by request.getInputStream(), because you’ve already consumed it beforehand! The client ain’t going to re-send the file for a second time so that you can read it for a second time (using Apache Commons FileUpload).

    Remove that 1st block altogether and it should work.

    Oh, please stop reading roseindia.net. That site only shows misleading code examples and teaches extremely bad practices which confuses starters altogether.

    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

I have two jsp pages and one servlet. When user clicks submit button then
I have a JSP page which display a list from servlet, it has a
I have a JSP page that has 2 forms running on Tomcat 6. One
I have a JSP page, which accepts user strings in more than 23 languages.
I have a JSP page, so the user can insert the time when he/she
I want to redirect JSP page from one servlet. All JSP pages are under
I have a .jsp page that passes the variable from the servlet. Lets assume
I have jsp page - <html> <head> </head> <body> <a href=http://localhost:8080/MyProject/Servlet123?usrID=33333>Go to servlet</a> </body>
I have servlet which does a forward using requestdispatcher() to one of my jsp
I have a jsp page which contains a form which calls a servlet, after

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.