Upload client file to servlet

Posted on 2006-04-13
Last Modified: 2013-11-24
 What I would like to accomplish is....
 create web page that allows user to upload a client file (Excel) to servlet for validation.
 so there are 2 parts - web form for upload, servlet to recieve and validate.
 There are 2 issues

 * given the web form and servlet below -  when the file is selected, and submitted I
get back an error page which is shown in Fig3. If I refresh the page, then the servlet responds.
I do not understand why I get the error.

* What do I need to include in my servlet to recieve the file as an IOstream, and to parse the file.
   I do not know where to start. I am using jakarta HSSF to read the excel file, and have this part working
  if reading local file. Just do not know how to setup the servlet to read the input file from client.

Any help would be greatly appreciated...
//My Web form - Fig1
    <FORM NAME=myform METHOD=POST ACTION= '' ENCTYPE="multipart/form-data">
       <INPUT TYPE=File SIZE=15 ID=parameterlist NAME=parameterlist>
       <input type="submit" value="Submit">

//Servlet - Fig 2

public class myTestServlet extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

    public void init(ServletConfig config) throws ServletException {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException, IOException {response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        out.println("<p>The servlet has received a GET. This is the reply.</p>");

//Error - Fig3

 The page cannot be displayed
The page you are looking for cannot be displayed because the address is incorrect.  
Please try the following:
If you typed the page address in the Address bar, check that it is entered correctly.
Open the home page and then look for links to the information you want.
Click  Search to look for information on the Internet.

HTTP 405 - Resource not allowed
Internet Explorer  
Question by:RockyFullen
    LVL 27

    Expert Comment

    You can find many previously ask questions in this java area and the JSP area that will help.  But most of them are for version 1.0  .  You should download 1.1 version now though.  
    here is one  example  (look at the last code I posted)
    LVL 2

    Expert Comment

    on client side default j1.5 package do no allow to upload file.... u hve to use external libraries to upload it....
    try the following struts library....
    hope this will help
    LVL 23

    Expert Comment


    Author Comment

      I tried your code posted in
    but get this error


            Missing class:

          Dependent class: org.apache.commons.fileupload.disk.DiskFileItem
                   Loader: current-workspace-app.web.Application1-eMos-webapp:0.0.0
              Code-Source: /D:/Program Files/Java/jdevstudio1013/jdev/lib/commons-fileupload-1.1.jar
            Configuration: <classpath> in D:\Program Files\Java\jdevstudio1013\jdev\mywork\Application1\eMos\public_html

    Author Comment

    Ok, There is a problem with this solution for those using jakarta fileUpload 1.1.

    DiskFileUpload has been deprecated, and replaced with ServletFileUpload.
    Not only has it been deprecated, but it will flat out not work.

    The new package commons-fileupload-1.1.jar does not include
    So, replace DiskFileUpload with ServletFileUpload to get around this in version 1.1

    Author Comment

    I do not know what I was thinking, rrz's example does not even use DiskFileUpload.
    Anyway, I was using a version that did, and thats how I found the problem I mentioned.
    I tried your version with a version of the html side I got from somewhere else.

    Now that I have the file
    List  items = upload.parseRequest(reqContext);
    How do I write it to a file?
    LVL 27

    Accepted Solution

    You can use something like the following.
    Iterator iter = items.iterator();
    while (iter.hasNext()) {
        FileItem item = (FileItem);
        if (item.isFormField()) {
        } else {
           // you could make some additional checks. like fieldName from client or file size.
            item.write(new File(pathPathYouWant));

    Let us know if you have any more questions about this.   rrz


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now