[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Upload client file to servlet

Posted on 2006-04-13
Medium Priority
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 28

Expert Comment

ID: 16446100
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)

Expert Comment

ID: 16447261
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

by:Siva Prasanna Kumar
ID: 16447498
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 16447537
  I tried your code posted in
but get this error


        Missing class: org.apache.commons.io.FileCleaner

      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

ID: 16447849
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 org.apache.commons.io.FileCleaner
So, replace DiskFileUpload with ServletFileUpload to get around this in version 1.1

Author Comment

ID: 16448109
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 28

Accepted Solution

rrz earned 2000 total points
ID: 16449537
You can use something like the following.
Iterator iter = items.iterator();
while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();
    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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

873 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