File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes problem in using Servlet API for file upload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem in using Servlet API for file upload" Watch "problem in using Servlet API for file upload" New topic
Author

problem in using Servlet API for file upload

yoman sharma
Greenhorn

Joined: Feb 10, 2005
Posts: 13
--------------------------------------------------------------------------------
Hi
Actually i have downloaded a JAR file from http://jakarta/apache/commons/fileupload.

I have also placed the jar file in jspbook/WEB-INF/lib/ folder where jspbook is the name of my web-application.
I am trying file upload through a HTML file named file upload.html which has following code. actually i am accessing servlet FileUploadCommons though this HTML file and using an API for file upload present in that JAR file.

<html>
<head>
<title>Example HTML Form</title>
</head>
<body>
<p>Select a file to upload or <a href="/jspbook/files/">browse
currently uploaded files.</a></p>
<form action="http://127.0.0.1/jspbook/FileUploadCommons"
method="post" enctype="multipart/form-data">
File: <input type="file" name="file"><br>
<input value="Upload File" type="submit">
</form>
</body>
</html>



The source code for servlet FileUploadCommons is as follows ::::


package com.jspbook;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import java.util.*;

public class FileUploadCommons extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html>");
out.print("File upload success. <a href=\"/jspbook/files/");
out.print("\">Click here to browse through all uploaded ");
out.println("files.</a><br>");

ServletContext sc = getServletContext();
String path = sc.getRealPath("/files");
org.apache.commons.fileupload.FileUpload fu = new
org.apache.commons.fileupload.FileUpload();
fu.setSizeMax(-1);
fu.setRepositoryPath(path);
try {
List l = fu.parseRequest(request);
Iterator i = l.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem)i.next();
// trim out full path info if it is included
String filename = fi.getName();
int slash = filename.lastIndexOf("\\");
if (slash != -1) {
filename = filename.substring(slash + 1);
}
// write the file to the 'files' directory
fi.write(path+"/"+filename);
}
}
catch (Exception e) {
throw new ServletException(e);
}

out.println("</html>");
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doPost(request, response);
}
}



Also i am using Tomcat 5 as the web server.


But when i try to upload the file by running the HTML file i am getting the following ERROR MESSAGE :


HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception


root cause

java.lang.NoSuchMethodError: org.apache.commons.fileupload.FileUpload.setSizeMax(I)V
com.jspbook.FileUploadCommons.doPost(Unknown Source)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.0.28




Please tell me what to do?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410



The error message told you what the problem is.

You're calling a method that doesn't exist.
Find out what the right spelling is for the method that allows you to set the maximum size.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
yoman sharma
Greenhorn

Joined: Feb 10, 2005
Posts: 13
I have downloaded the source as well as class files from Jakarta website
http://jakarta/apache/commons/fileupload

Its a already built API.

Then why is the error message. what can you say about the other errors?
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by yoman sharma:
I have downloaded the source as well as class files from Jakarta website
http://jakarta/apache/commons/fileupload

Its a already built API.

Then why is the error message. what can you say about the other errors?


see http://www.theserverside.com/discussions/thread.tss?thread_id=22816
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
This method, setSizeMax(long), is mentioned in the docs and the compiler is not giving me any error on this. couldn't say more.
yoman sharma
Greenhorn

Joined: Feb 10, 2005
Posts: 13
I am posting you the files at your e mail id. adeel.gnome@gmail.com please check by running

1. fileupload.html
2. FileUploadCommons.java
3. FileUploadCommons.class

The API you can get from http://jakarta/apache/commons/fileupload

Please check and tell.
yoman sharma
Greenhorn

Joined: Feb 10, 2005
Posts: 13
Can anybody rectify this problem?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in using Servlet API for file upload