1. Overview

Commons FileUpload 라이브러리를 이용한 파일 업로드 예제 어플리케이션

Uploading-File-With-Servlet 를 작성하며 알게 되었는데, Servlet 3.0 부터 외부 라이브러리 없이 request.getParts() 로 가능하다.

2. Description

배포할 /sw/app/fileUpload 어플리케이션 구조는 다음과 같다.

1
2
3
4
5
6
7
8
9
10
11
12
13
/sw/app/fileUpload/
├── META-INF
└── WEB-INF
    ├── classes
    │   └── example
    │       └── dong
    │           └── FileUploadServlet.class
    ├── src
    │   └── example
    │       └── dong
    │           └── FileUploadServlet.java
    ├── weblogic.xml
    └── web.xml

xml file에는 별다른 내용이 없다.

FileUploadServlet.java 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package example.dong;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;


@WebServlet(name = "FileUploadServlet", urlPatterns = { "/fileuploadservlet" })
@MultipartConfig(
  fileSizeThreshold = 1024 * 1024 * 10,     // 10 MB
  maxFileSize = 1024 * 1024 * 1024 * 1,    // 1 GB
  maxRequestSize = 1024 * 1024 * 1024 * 1  // 1 GB
)
/* Simple Java File Upload Example */
public class FileUploadServlet extends HttpServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    /* For comparison of Servlet and Apache Commons File Upload APIs */
    Part filePart = request.getPart("file");
    String fileName = filePart.getSubmittedFileName();
    for (Part part : request.getParts()) {
      part.write(fileName);
    }
    response.getWriter().print("Sucessfully Java file upload. -> " + filePart.getSize());
  }

}

maxFileSize = maxRequestSize = 1024 * 1024 * 1024 * 10 과 같이 10GB 으로 설정하면 Exception 발생한다.

1
The field file exceeds its maximum permitted size of -2147483648 characters.

Field 가 수용가능한 범위는 int32 이며, integer 32bit 범위는 -2,147,483,648 ~ 2,147,483,647 이다.

maxFileSize = maxRequestSize = 2147483647 을 설정하여 최대 한계인 2GB File upload 까지 가능하다.

maxFileSize = maxRequestSize = -1 을 설정하면, Unlimited 이다.

배포 후 curl 로 테스트 한다.

1
2
$ curl -F 'file=@/tmp/test.txt' http://../fileUpload/fileuploadservlet
Sucessfully Java file upload. -> 10485760

참고로, test.txt (10 MB)와 같은 파일은 다음의 명령으로 간편하게 생성할 수 있다.

1
$ dd if=/dev/zero of=test.txt bs=1 count=0 seek=10M