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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:30:14+00:00 2026-05-25T11:30:14+00:00

I wrote a HTML code to upload a file in jsp.now i have to

  • 0

I wrote a HTML code to upload a file in jsp.now i have to take this data and work with it using a java program in jsp.can you please provide me a sample code of how to achieve this.

Thank you

  • 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-25T11:30:15+00:00Added an answer on May 25, 2026 at 11:30 am

    I’m note sure about your code. Please follows these steps

    1. Download the binary distribution of apache commons-fileupload and apache commons-io (FileUpload depends on Commons IO, so make sure you have the version mentioned on the dependencies page in your classpath before continuing.)

    2. Extract both these distributions and copy commons-fileupload-1.2.2.jar and commons-io-2.0.1.jar under WEB-INF/lib folder.

    3. In the Servlet, you need to import javax.servlet,javax.servlet.http, import java.io, java.util,org.apache.commons.fileupload,org.apache.commons.fileupload.servlet and org.apache.commons.fileupload.disk packages.

    Demo:

    Create a servlet file under WEB-INF/classes folder

    package com.me;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.*;
    import java.io.IOException;
    import java.io.File;
    import java.util.List;
    import java.util.Iterator;
    import org.apache.commons.fileupload.FileItemFactory;
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    
    
    public class FileUploadServlet extends HttpServlet
    {
      public void doPost(HttpServletRequest request,
                         HttpServletResponse response) 
                             throws ServletException, IOException
       {
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            java.io.PrintWriter pw=response.getWriter();
            final String SUCCESS="/success.jsp";
            final String FAIL="/fail.jsp";
            if(isMultipart) 
             {
              try
               {
    
                 FileItemFactory factory = new DiskFileItemFactory();
                 ServletFileUpload upload = new ServletFileUpload(factory);
                 List items = upload.parseRequest(request);
                 pw.print("total : " + items.size());
                 Iterator iter = items.iterator();
                 while (iter.hasNext()) 
                 {
                    FileItem item = (FileItem) iter.next();
                 pw.print("total : " + item);
                    if(!item.isFormField()) 
                      {
                         String fileName = item.getName();
                         String destFilePath=getServletContext().getRealPath("/upload/" + fileName);
                         File uploadedFile = new File(destFilePath);
                         item.write(uploadedFile);
                       }
                  }
                 request.setAttribute("message","file has been uploaded successfully!");
               }catch(Exception ex)
               {
    
                  ex.printStackTrace();
                  request.setAttribute("message","Cannot upload : " + ex.getMessage());
                  getServletContext().getRequestDispatcher(FAIL).forward(request,response);
               }
               getServletContext().getRequestDispatcher(SUCCESS).forward(request,response);
            }
       }
    }
    

    Configure/register the servlet in WEB-INF/web.xml

    <web-app>
     <servlet>
        <servlet-name>fileupload</servlet-name>
        <servlet-class>com.me.FileUploadServlet</servlet-class>
     </servlet>
     <servlet-mapping>
        <servlet-name>fileupload</servlet-name>
        <url-pattern>/fileupload</url-pattern>
     </servlet-mapping>
    </web-app>
    

    Content of the view (upload.jsp).

    NOTE: Set enctype attribute of with “multipart/form-data”

    <form method="post" enctype="multipart/form-data" action="fileupload">
      <input type="file" name="file"/>
      <input type="submit"/>
    </form>
    

    Content of success.jsp and fail.jsp

    ${message}
    

    Create a folder named upload at root of context.

    JSP version (This is not a recommended way)

    upload1.jsp

    <form method="post" enctype="multipart/form-data" 
    
    action="showfile.jsp">
      <input type="file" name="file"/>
      <input type="submit"/>
    </form>
    

    showfile.jsp

    <%@ page import="java.io.*" %>
    <%@ page import="java.util.List" %>
    <%@ page import="java.util.Iterator" %>
    <%@ page import="org.apache.commons.fileupload.FileItemFactory" %>
    <%@ page import="org.apache.commons.fileupload.FileItem" %>
    <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
    <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
    
    
    <%
    
     boolean isMultipart = ServletFileUpload.isMultipartContent(request);
     String content="";
    
     if(isMultipart) 
         {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = upload.parseRequest(request);
    
            Iterator iter = items.iterator();
            while (iter.hasNext()) 
             {
                FileItem item = (FileItem) iter.next();
                if(!item.isFormField()) 
                  {
                     BufferedInputStream buff=new BufferedInputStream(item.getInputStream());
                     byte []bytes=new byte[buff.available()];
                     buff.read(bytes,0,bytes.length);
                     content=new String(bytes);
                  }
              }
         }
    %>
    
    Content of File:
    <pre>
     <%=content%>
    </pre>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i wrote some html code and want to edit its view using the css
For some reason my PDF upload form is failing consistently, I have this code:
I m embeding a pdf document into my html code. For this i have
I am not able to upload multiple files.I am using this code. Here itr.hasNext()
I have a html file which select images from user's computer.code is given bellow
I have HTML page, which gives an option to upload a file to the
Want to upload a file using ajax for this using this uploader http://valums.com/ajax-upload/ and
I have an html page and I want to upload a file usign the
I have this little set of Python code on the GAE, trying to upload
I wrote some CSS in my HTML code to create rollover buttons. Then i

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.