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 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

problem in using Servlet API

yoman sharma
Greenhorn

Joined: Feb 10, 2005
Posts: 13
--------------------------------------------------------------------------------
Hi
Actually i have downloaded a JAR file.
I have also placed the jar file in jspbook/WEB-INF/lib/ folder .
I am trying file upload through a HTML file 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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in using Servlet API