2

I am getting a java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream when tring to upload a file to my server runnning on localhost. This is the servlet I am using:

@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private String UPLOAD_DIRECTORY = "~/EclipseProjects/DocSearch/uploads";

/**
 * @see HttpServlet#HttpServlet()
 */
public UploadServlet() {
    super();
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if(ServletFileUpload.isMultipartContent(request)) {
        try {
            List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
            for(FileItem item: multiparts) {
                if(!item.isFormField()) {
                    String name = new File(item.getName()).getName();
                    item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
                }
            }
            request.setAttribute("message", "File uploaded successfully");
        } catch (Exception ex) {
            request.setAttribute("message", "File upload failed due to: " + ex);
        }
    } else {
        request.setAttribute("message", "Sorry this servlet only handles file upload requests");
    }
    request.getRequestDispatcher("/result.jsp").forward(request, response);
}

}

I have already added commons-fileupload-1.2.2.jar into the WEB-INF/lib folder. I have tried creating a new server and running the project on it, tried re-downloading the jar file from different sources, but none of them seems to solve my problem.

Note:

I have already gone through:

Community
  • 1
  • 1
shyam
  • 1,201
  • 3
  • 20
  • 37

1 Answers1

1

You must include commons-fileupload.jar into your project dependency library folder.

Get commons-fileupload.jar from official website – http://commons.apache.org/proper/commons-fileupload/download_fileupload.cgi or from maven web site:http://mvnrepository.com/artifact/commons-fileupload/commons-fileupload/1.3.1

if you use maven add this dependency to your pom.xml:

 <dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>
CHHIBI AMOR
  • 1,046
  • 1
  • 13
  • 25
  • Unfortunately I haven't used maven build for this particular project but I have tried adding the jar in the java build path in the project properties. – shyam Dec 26 '14 at 08:34
  • @shyam download the jar from appache web site and add it in you project build pth. – CHHIBI AMOR Dec 26 '14 at 08:42
  • Yes. That did the trick. I'll accept your answer if you edit out the link you have given. (It's commons-fileupload not commons-io). Still curious to why the previous jars didn't work though. I downloaded it from maven central actually. – shyam Dec 26 '14 at 09:00