Commons FileUpload Example with HTML Pages


Where to find a good working example on using Commons FileUpload library with HTML pages?



If you are looking a good working example on Commons FileUpload library with HTML pages, you can read the How do I create a web based file upload Web page.

Here is the Java source code of the Servlet that uses the Commons FileUpload API:

package org.kodejava.example.commons.fileupload;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;

public class FileUploadDemoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);

        if (isMultipart) {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);

            try {
                List items = upload.parseRequest(request);
                Iterator iterator = items.iterator();
                while (iterator.hasNext()) {
                    FileItem item = (FileItem);

                    if (!item.isFormField()) {
                        String fileName = item.getName();

                        String root = getServletContext().getRealPath("/");
                        File path = new File(root + "/uploads");
                        if (!path.exists()) {
                            boolean status = path.mkdirs();

                        File uploadedFile = new File(path + "/" + fileName);
            } catch (FileUploadException e) {
            } catch (Exception e) {


Commons FileUpload Example with JSP Pages

Using commons-fileupload.jar with Tomcat

Using commons-fileupload.jar with Tomcat

⇑⇑ FAQ for Apache commons-fileupload.jar

2020-08-09, 992🔥, 0💬