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

x
?
Solved

Upload client file to servlet

Posted on 2006-04-13
9
Medium Priority
?
299 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  
0
Comment
Question by:RockyFullen
7 Comments
 
LVL 28

Expert Comment

by:rrz
ID: 16446100
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
0
 
LVL 2

Expert Comment

by:amol_chaudhari
ID: 16447261
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
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16447498
0
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

by:RockyFullen
ID: 16447537
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
0
 

Author Comment

by:RockyFullen
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
0
 

Author Comment

by:RockyFullen
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?
0
 
LVL 28

Accepted Solution

by:
rrz earned 2000 total points
ID: 16449537
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

0

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