Search in sources :

Example 1 with SizeLimitExceededException

use of org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException in project JessMA by ldcsaa.

the class FileUploader method upload.

/** 执行上传
	 * 
	 * @param request	: {@link HttpServletRequest} 对象
	 * @param response	: {@link HttpServletResponse} 对象
	 * 
	 * @return			: 成功:返回 {@link Result#SUCCESS} ,失败:返回其他结果,
	 * 					     失败原因通过 {@link FileUploader#getCause()} 获取
	 * 
	 */
public Result upload(HttpServletRequest request, HttpServletResponse response) {
    reset();
    String absolutePath = getAbsoluteSavePath(request);
    if (absolutePath == null) {
        cause = new FileNotFoundException(String.format("path '%s' not found or is not directory", savePath));
        return Result.INVALID_SAVE_PATH;
    }
    ServletFileUpload sfu = getFileUploadComponent();
    List<FileItemInfo> fiis = new ArrayList<FileItemInfo>();
    List<FileItem> items = null;
    Result result = Result.SUCCESS;
    String encoding = servletHeaderencoding != null ? servletHeaderencoding : request.getCharacterEncoding();
    FileNameGenerator fnGenerator = fileNameGenerator != null ? fileNameGenerator : DEFAULT_FILE_NAME_GENERATOR;
    try {
        items = (List<FileItem>) sfu.parseRequest(request);
    } catch (FileUploadException e) {
        cause = e;
        if (e instanceof FileSizeLimitExceededException)
            result = Result.FILE_SIZE_EXCEEDED;
        else if (e instanceof SizeLimitExceededException)
            result = Result.SIZE_EXCEEDED;
        else if (e instanceof InvalidContentTypeException)
            result = Result.INVALID_CONTENT_TYPE;
        else if (e instanceof IOFileUploadException)
            result = Result.FILE_UPLOAD_IO_EXCEPTION;
        else
            result = Result.OTHER_PARSE_REQUEST_EXCEPTION;
    }
    if (result == Result.SUCCESS) {
        result = parseFileItems(items, fnGenerator, absolutePath, encoding, fiis);
        if (result == Result.SUCCESS)
            result = writeFiles(fiis);
    }
    return result;
}
Also used : InvalidContentTypeException(org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) FileSizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException) FileItem(org.apache.commons.fileupload.FileItem) SizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException) FileSizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) IOFileUploadException(org.apache.commons.fileupload.FileUploadBase.IOFileUploadException) IOFileUploadException(org.apache.commons.fileupload.FileUploadBase.IOFileUploadException) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 2 with SizeLimitExceededException

use of org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException in project fess by codelibs.

the class FessMultipartRequestHandler method handleRequest.

// ===================================================================================
//                                                                      Handle Request
//                                                                      ==============
@Override
public void handleRequest(final HttpServletRequest request) throws ServletException {
    // /- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // copied from super's method and extends it
    // basically for JVN#14876762
    // thought not all problems are resolved however the main case is safety
    // - - - - - - - - - -/
    final ServletFileUpload upload = createServletFileUpload(request);
    prepareElementsHash();
    try {
        final List<FileItem> items = parseRequest(request, upload);
        mappingParameter(request, items);
    } catch (final SizeLimitExceededException e) {
        handleSizeLimitExceededException(request, e);
    } catch (final FileUploadException e) {
        handleFileUploadException(e);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) SizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 3 with SizeLimitExceededException

use of org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException in project fess by codelibs.

the class FessMultipartRequestHandler method handleSizeLimitExceededException.

protected void handleSizeLimitExceededException(final HttpServletRequest request, final SizeLimitExceededException e) {
    final long actual = e.getActualSize();
    final long permitted = e.getPermittedSize();
    final String msg = "Exceeded size of the multipart request: actual=" + actual + " permitted=" + permitted;
    request.setAttribute(MAX_LENGTH_EXCEEDED_KEY, new MultipartExceededException(msg, actual, permitted, e));
    try {
        final InputStream is = request.getInputStream();
        try {
            final byte[] buf = new byte[1024];
            while ((is.read(buf)) != -1) {
            }
        } catch (final Exception ignored) {
        } finally {
            try {
                is.close();
            } catch (final Exception ignored) {
            }
        }
    } catch (final Exception ignored) {
    }
}
Also used : InputStream(java.io.InputStream) MultipartExceededException(org.lastaflute.web.ruts.multipart.exception.MultipartExceededException) ServletException(javax.servlet.ServletException) MultipartExceededException(org.lastaflute.web.ruts.multipart.exception.MultipartExceededException) SizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException) Forced404NotFoundException(org.lastaflute.web.exception.Forced404NotFoundException) IOException(java.io.IOException) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

SizeLimitExceededException (org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException)3 FileUploadException (org.apache.commons.fileupload.FileUploadException)3 FileItem (org.apache.commons.fileupload.FileItem)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 ServletException (javax.servlet.ServletException)1 FileSizeLimitExceededException (org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException)1 IOFileUploadException (org.apache.commons.fileupload.FileUploadBase.IOFileUploadException)1 InvalidContentTypeException (org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException)1 Forced404NotFoundException (org.lastaflute.web.exception.Forced404NotFoundException)1 MultipartExceededException (org.lastaflute.web.ruts.multipart.exception.MultipartExceededException)1