File Upload problem
Hi,
I get this following exception while trying to upload. I have imported
<%@ page import="org.apache.commons.fileupload.*"%>
I get error at this point
// Create a factory for disk-based file items
FileItemFactory factory =new DiskFileItemFactory();
exception
-
javax.servlet.ServletException: org.apache.commons.io.FileCleaner.getInstance()Lorg/apache/commons/io/FileCleaningTracker;
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.FileCopy_jsp._jspService(FileCopy_jsp.java:123)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
root cause
java.lang.NoSuchMethodError: org.apache.commons.io.FileCleaner.getInstance()Lorg/apache/commons/io/FileCleaningTracker;
org.apache.commons.fileupload.disk.DiskFileItemFactory.<init>(DiskFileItemFactory.java:104)
org.apache.jsp.FileCopy_jsp._jspService(FileCopy_jsp.java:71)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
should i change the web.xml file. It doc they have mentioned that following should be included in web.xml.
<listener>
<listener-class>
org.apache.commons.fileupload.servlet.FileCleanerCleanup
</listener-class>
</listener>
iam using netbeans and if i include above in my web.xml, my project is not working.
where did i go wrong. First time iam using this package to upload. Just started. but iam stuck with this.
Thanks,
Thanuja.
[2700 byte] By [
thanua] at [2007-11-27 9:23:26]

# 1
any suggestion?
thanua at 2007-7-12 22:18:34 >

# 2
hi thanu... try this...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- upload.jsp -->
<%@ page import="java.io.*" %>
<%
//String path = application.getRealPath("/") + "ADP\C:/\";
//out.println(path);
//String pathSeparator = File.separator; // will return either "\" or "/", depends on OS
//out.println(pathSeparator);
//String myFilePath = "some dir" + pathSeparator + "myfile.txt";
//String fullPath = path + myFilePath;
String contentType = request.getContentType();
out.println("Content type is :: " +contentType);
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.print("FileName:" + saveFile.toString());
//out.print(dataBytes);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
saveFile = "C:\\test\\" + saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);
//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
//boolean isDeleted = new File(saveFile).delete();
//out.println("isDeleted? : " + isDeleted );
//out.println("File saved as " +saveFile);
}
%>
||You need to create a form for file selection and set the appropriate encoding:
<form action="upload.jsp" method="POST" enctype="multipart/form-data">
<!-- enctype="multipart/form-data" -->
<input type="file" name="theFile">
<input type="submit">
</form>
Message was edited by:
ravi-kumar-123
# 3
Hi Thanu, It Seems to be you do not have the Commons IO jar in your classpath. Make sure that your jar should be inside the your application's web-inf\lib folder. good luck;Thanksismail
# 4
yes i have included commons.fileupload jar and commons.io jar in classpath.
now i get this error:
exception
javax.servlet.ServletException: javax/servlet/http/HttpServletRequest
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.FileCopy_jsp._jspService(FileCopy_jsp.java:160)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
root cause
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(ServletFileUpload.java:68)
org.apache.jsp.FileCopy_jsp._jspService(FileCopy_jsp.java:67)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
really i could not figure out whats wrong.
thanua at 2007-7-12 22:18:34 >

# 5
Look at the root cause problem -- you are still missing a required class (a different one this time though). In fact, I think that one can be found in one of Tomcat's jars.....
# 6
ok fine. i have added all jar files in WEB-INF\lib
i have chkd again and again. But still i get this error.
javax.servlet.ServletException: javax/servlet/http/HttpServletRequest
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.FileCopy_jsp._jspService(FileCopy_jsp.java:109)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
root cause
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(ServletFileUpload.java:68)
org.apache.jsp.FileCopy_jsp._jspService(FileCopy_jsp.java:59)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
i have placed both commons.fileupload jar and commons io jar in web-inf/lib.
thanua at 2007-7-12 22:18:34 >

# 7
Add servlet.jar, or j2ee.jar, or javaee.jar (depending on the version of the running environment used) to the library of your JRE.
# 8
hai i copied the j2ee.jar in jre/lib but still i get the same error.
thanua at 2007-7-12 22:18:34 >

# 9
Then you're using wrong JRE or misunderstanding it.Are you using an IDE? E.g. Eclipse?
# 10
Hi Thanu,
try any of the following;I have succeded in both the cases(but i am using Web tools Eclipse IDE)
1.Import/copy all the required dependencies/jars(servelet-api,file upload etc) into your application web-inf/lib
folder;Dont add into project class path or jre.
2.place the all jar file following folder
%JAVA_HOME%\jre\lib\ext\
and us this import statement, u dont need anything else
import org.apache.commons.fileupload.*;
NOTE:- Make sure that JAVA_HOME should be pointed to SDK(not JRE)
best regards
ismail
# 11
No. Don't add it to the WEB-INF/lib or /jre/lib/ext on the filesystem manually.
If you're using Eclipse, it was sufficient to go to Window ?Preferences ?Java ?Installed JRE and select the currently used JRE, press Edit and add the JAR to the listing of JRE System Libraries. Then this JAR will be used in every project which uses the specified JRE. The servlet.jar is generally already available in the /lib of the applicationserver, so there's no need to add it to the appserver.
# 12
yes iam using netbeans
thanua at 2007-7-12 22:18:34 >

# 13
ive placed the servlet.jar. now i get the following error which i got initially.
exception
javax.servlet.ServletException: org.apache.commons.io.FileCleaner.getInstance()Lorg/apache/commons/io/FileCleaningTracker;
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.FileCopy_jsp._jspService(FileCopy_jsp.java:123)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
root cause
java.lang.NoSuchMethodError: org.apache.commons.io.FileCleaner.getInstance()Lorg/apache/commons/io/FileCleaningTracker;
org.apache.commons.fileupload.disk.DiskFileItemFactory.<init>(DiskFileItemFactory.java:104)
org.apache.jsp.FileCopy_jsp._jspService(FileCopy_jsp.java:71)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
i have placed commons io in web-inf/lib. then why still i get this error?
Thanuja.
thanua at 2007-7-12 22:18:34 >

# 14
If the class cannot be found, then you should get a NoClassDefFoundError or ClassNotFoundException, depending on type call.
But you're getting a NoSuchMethodError, which means that the class can be found, but that it does not contain the given method.
This is generally caused by version conflicts. Try to download the newest versions from apache.org for this stuff and retry.
# 15
hi thanks for ur reply balu. iam using commons fileupload 1.2and commons io 1.3.1. Is this the right version?
thanua at 2007-7-21 23:00:13 >

# 16
Try commons io 1.3.2A quick glance at the APIs available showed the getInstance() method in the 1.3.2 (current latest) but not in the 1.3.1 http://jakarta.apache.org/commons/io/
# 17
Check the manfacturer's site what's the latest version: http://jakarta.apache.org/commons/io/The release notes by the way states that FileCleaningTracker was introduced in 1.3.2.
# 18
yes evanfets and Balu . Thank u guys. Well i got the solution. yes the problem was with the version. when i chked this link http://jakarta.apache.org/commons/io/ for download it said current release 1.3.1. so i downloaded that. oh my god at last i got a solution. thanks a lot.
5 dukes to evanfets and 5 dukes to Balu.
Thanks
Thanuja.
thanua at 2007-7-21 23:00:14 >

# 19
hey can u suggest me a right solution for this thread http://forum.java.sun.com/thread.jspa?threadID=5191150Thanks,thanuja
thanua at 2007-7-21 23:00:14 >
