Servletでmultipart/form-dataを受け取る

Servletでmultipart/form-dataを受け取るにはJakarta Commons FileUploadを利用するのが簡単です。

サンプル

ServletFileUpload upload =
  new ServletFileUpload(new DiskFileItemFactory());

List items;
try {
  items = upload.parseRequest(servletRequest);
} catch (FileUploadException e) {
  throw new ServletException(e);
}

for (Object val : items) {
  FileItem item = (FileItem) val;
  if (item.isFormField()) {
    System.out.println("form data ("
      + item.getFieldName() + ": " + new String(item.get())
      + ")");
  } else {
    System.out.println("file found!");
  }
}

参考