How to Upload Files with Apache Commons FileUpload

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

@SuppressWarnings("serial")
public final class SampleServlet extends HttpServlet {

  @Override
  protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException {

    try {
      final ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());

      final List<FileItem> itemLst = upload.parseRequest(req);

      ...
    } catch (final Exception e) {
      throw new ServletException("Unable to receive files", e);
    }

  }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s