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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:53:29+00:00 2026-05-18T02:53:29+00:00

Note: Before reading this question and its answer, please check your input element has

  • 0

Note:

Before reading this question and its answer, please check your input element has name attribute.

I am trying to upload file using servlet. Eclipse console shows no errors. But the file is not getting uploaded. For me, it seems everything is fine. But I am making mistake somewhere.

In console I get just

Inside Servlet //Printed by code
Items: [] // Printed by Cdoe

Html Code:

<form action="ImageUploadServlet" method="post" enctype="multipart/form-data">
<table>
<tr>
  <td><label>Select Image: </label></td>
  <td><input type="file" id="sourceImage" /></td>
   <tr>
        <td colspan="3">
         <input type="submit" value="Upload"/><span id="result"></span>
        </td>
      </tr> 
</table>
</form>

Servlet Code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
    System.out.println("Inside Servlet");
    if(!isMultiPart){
        System.out.println("Form type is not multipart/form-data");
        System.out.println("File Not Uploaded");
    }
    else
    {
        FileItemFactory dfit = new DiskFileItemFactory();
        ServletFileUpload sfu = new ServletFileUpload(dfit);
        List aList = null;

        try {
            aList = sfu.parseRequest(request);
            System.out.println("Items: "+aList);
        } 
        catch (FileUploadException fue) 
        {
            fue.printStackTrace();
        }

        Iterator itr = aList.iterator();
        while(itr.hasNext())
        {
            FileItem fi = (FileItem) itr.next();
            if(fi.isFormField())
            {
                System.out.println("File Name: "+fi.getFieldName());
                System.out.println("File Size: "+fi.getSize());

                try 
                {
                    File f = new File("D:/MyUploads/", fi.getName());
                    fi.write(f);
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
            else
            {
                System.out.println("It's Not Form Item;");
            }
        }
    }
}
}

Any suggestions would be appreciated.

Thanks!

  • 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-18T02:53:30+00:00Added an answer on May 18, 2026 at 2:53 am

    There are two problems:

    First, you need to give the field a name. It becomes then the request parameter name.

    <input type="file" id="sourceImage" name="sourceImage" />
    

    Second, you need to handle the file uploads in the else case of FileItem#isFormField() as per the commons fileupload guide. Your code is currently ignoring them and doing only a sysout.

    if (item.isFormField()) {
        // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
        String fieldname = item.getFieldName();
        String fieldvalue = item.getString();
        // ... (do your regular form field processing job here)
    } else {
        // Process form file field (input type="file").
        String fieldname = item.getFieldName();
        String filename = FilenameUtils.getName(item.getName());
        // ... (do your uploaded file job here)
        File file = new File("D:/MyUploads/", filename);
        item.write(file);
    }
    

    Note that you need to use FilenameUtils#getName() to extract the file name, because MSIE browser incorrectly sends the full client side file path along the file name. See also Commons FileUpload FAQ.

    You also need to keep in mind that this approach will overwrite any previously uploaded file with the same name. You may want to add an autogenerated suffix to the filename.

    See also:

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

Sidebar

Related Questions

Before reading please note that I've googled this and read a ton of articles
NOTE : Right before posting this question it occurred to me there's a better
Note this question was originally posted in 2009, before C++11 was ratified and before
Please note that this is not homework and i did search before starting this
Reading this question I found this as (note the quotation marks) code to solve
Before reading, please note that I am very new to both PHP and MYSQL.
Before I ask this, do note: I want this for debugging purposes. I know
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
(Note: This is not a question about what is the best way with code
Note: Not sure if this is the right stack, please tell if I should

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.