We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Upload client file to servlet

RockyFullen
RockyFullen asked
on
Medium Priority
318 Views
Last Modified: 2013-11-24
Hi,
 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
<body>
    <FORM NAME=myform METHOD=POST ACTION= 'http://10.10.10.10:8988/Application1-eMos-context-root/mytestservlet' ENCTYPE="multipart/form-data">
       <INPUT TYPE=File SIZE=15 ID=parameterlist NAME=parameterlist>
       <input type="submit" value="Submit">
    </FORM>
</body>

//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 {
        super.init(config);
    }

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


//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 10.10.10.10:8988 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  
Comment
Watch Question

rrzstudent
CERTIFIED EXPERT

Commented:
See    
http://jakarta.apache.org/commons/fileupload   
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)
http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_21753142.html
hi
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....
http://apache.secsup.org/dist/struts/binaries/struts-1.2.9-bin.tar.gz
hope this will help
bye
Siva Prasanna KumarPrincipal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:

Author

Commented:
rrz,
  I tried your code posted in
http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_21753142.html
but get this error

oracle.classloader.util.AnnotatedNoClassDefFoundError:

        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

Commented:
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

Commented:
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?
student
CERTIFIED EXPERT
Commented:
from  
http://jakarta.apache.org/commons/fileupload/using.html 
You can use something like the following.
Iterator iter = items.iterator();
while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();
    if (item.isFormField()) {
        processFormField(item);
    } 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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.