Struts 2 파일 업로드 크기 제한

struts 2 업로드 프레임 워 크 는 COS,pell,Common-fileUpload 등 몇 가지 가 있 습 니 다.struts.properties 설정 을 통 해 다음 과 같이 설정 할 수 있 습 니 다.
struts.multipart.parser=cos
struts.multipart.parser=pell
struts.multipart.parser=jakarta//struts 2 는 기본적으로 Jakarta 의 Common-fileUpload 파일 업로드 해상도 기 를 사용 합 니 다.
struts 2 자체 파일 default.properties 에 설 치 된(시스템 기본 크기 2M)struts.multipart.max Size=2097152 이것 은 struts.propertise 파일 에서 수정 할 수 있 습 니 다. 
Struts 2 는 먼저 Common-fileUpload 파일 업로드 해상도 기 를 통 해 파일 크기 를 걸 러 냅 니 다.
Common-fileUpload 패키지 의 FileUploadBase.class 에서 parseRequest 방법 에서 크기 제한 을 했 습 니 다.코드 는 다음 과 같 습 니 다.
        if (sizeMax >= 0 && requestSize > sizeMax) {
            throw new SizeLimitExceededException(
                "the request was rejected because its size (" + requestSize
                + ") exceeds the configured maximum (" + sizeMax + ")",
                requestSize, sizeMax);
        }

이곳 의 size Max 는 struts.multipart.max Size 의 값 입 니 다.파일 을 업로드 하기 전에 시스템 은 파일 의 크기 가 이 값 을 초과 하 는 지 비교 합 니 다.초과 하면 상기 이상 을 던 집 니 다.comons-fileupload 구성 요 소 는 국제 화 를 지원 하지 않 기 때문에 우리 가 본 이상 은 모두 기본 입 니 다.
그리고 struts 2 핵심 라 이브 러 리 의 org.apache.struts 2.intercepto.FileUploadInterceptor 에 들 어가 크기 를 차단 합 니 다.이 크기 는 struts.xml 에 설정 되 어 있 습 니 다.다음 과 같 습 니 다.
<interceptor-ref name="fileUpload">

<param name="maximumSize">409600</param>             400K
<param name="allowedTypes">...</param> mime  ,       

</interceptor-ref>
**         
<interceptor-ref name="defaultStack" />
FileUploadInterceptor.class 에서 필터 코드 는 다음 과 같 습 니 다.
    /**
     * Override for added functionality. Checks if the proposed file is acceptable based on contentType and size.
     *
     * @param file        - proposed upload file.
     * @param contentType - contentType of the file.
     * @param inputName   - inputName of the file.
     * @param validation  - Non-null ValidationAware if the action implements ValidationAware, allowing for better
     *                    logging.
     * @param locale
     * @return true if the proposed file is acceptable by contentType and size.
     */
    protected boolean acceptFile(File file, String contentType, String inputName, ValidationAware validation, Locale locale) {
        boolean fileIsAcceptable = false;

        // If it's null the upload failed
        if (file == null) {
            String errMsg = getTextMessage("struts.messages.error.uploading", new Object[]{inputName}, locale);
            if (validation != null) {
                validation.addFieldError(inputName, errMsg);
            }

            log.error(errMsg);
        } else if (maximumSize != null && maximumSize.longValue() < file.length()) {
            String errMsg = getTextMessage("struts.messages.error.file.too.large", new Object[]{inputName, file.getName(), "" + file.length()}, locale);
            if (validation != null) {
                validation.addFieldError(inputName, errMsg);
            }

            log.error(errMsg);
        } else if ((! allowedTypesSet.isEmpty()) && (!containsItem(allowedTypesSet, contentType))) {
            String errMsg = getTextMessage("struts.messages.error.content.type.not.allowed", new Object[]{inputName, file.getName(), contentType}, locale);
            if (validation != null) {
                validation.addFieldError(inputName, errMsg);
            }

            log.error(errMsg);
        } else {
            fileIsAcceptable = true;
        }

        return fileIsAcceptable;
    }
이곳 의 maximum Size 는 Struts.xml 의 maximum Size 를 말 합 니 다.설정 이 없 으 면 값 은 null 입 니 다.
fileUpload 차단 기 는 파일 이 서버 에 올 라 간 후에 만 파일 형식 과 크기 를 판단 합 니 다.업로드 한 파일 크기 가 딱 맞 으 면 struts.multipart.max Size 와 maximumSize 사이 에 key 를 struts.messages.error.file.too.large 에 대응 하 는 이상 정 보 를 던 집 니 다.
메모:maximumSize 는 struts.multipart.maxSize 보다 크 면 안 됩 니 다.그렇지 않 으 면 필터 효과 가 없습니다.

좋은 웹페이지 즐겨찾기