Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream [duplicate]

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:

  • How to solve this java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream?

  • javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream

  • FileUpload: DeferredFileOutputStream class not found?

like image 597
shyam Avatar asked Jul 05 '26 22:07

shyam


1 Answers

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>
like image 136
CHHIBI AMOR Avatar answered Jul 07 '26 13:07

CHHIBI AMOR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!