Search in sources :

Example 1 with FileSizeLimitExceededException

use of org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException 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 FileSizeLimitExceededException

use of org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException in project OpenClinica by OpenClinica.

the class FileUploadHelper method getFiles.

@SuppressWarnings("unchecked")
private List<File> getFiles(HttpServletRequest request, ServletContext context, String dirToSaveUploadedFileIn) {
    List<File> files = new ArrayList<File>();
    // FileCleaningTracker fileCleaningTracker =
    // FileCleanerCleanup.getFileCleaningTracker(context);
    // Create a factory for disk-based file items
    DiskFileItemFactory factory = new DiskFileItemFactory();
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setFileSizeMax(getFileProperties().getFileSizeMax());
    try {
        // Parse the request
        List<FileItem> items = upload.parseRequest(request);
        // Process the uploaded items
        Iterator<FileItem> iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = iter.next();
            if (item.isFormField()) {
                request.setAttribute(item.getFieldName(), item.getString());
            // DO NOTHING , THIS SHOULD NOT BE Handled here
            } else {
                getFileProperties().isValidExtension(item.getName());
                files.add(processUploadedFile(item, dirToSaveUploadedFileIn));
            }
        }
        return files;
    } catch (FileSizeLimitExceededException slee) {
        throw new OpenClinicaSystemException("exceeds_permitted_file_size", new Object[] { String.valueOf(getFileProperties().getFileSizeMaxInMb()) }, slee.getMessage());
    } catch (FileUploadException fue) {
        throw new OpenClinicaSystemException("file_upload_error_occured", new Object[] { fue.getMessage() }, fue.getMessage());
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ArrayList(java.util.ArrayList) FileSizeLimitExceededException(org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) File(java.io.File) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

ArrayList (java.util.ArrayList)2 FileItem (org.apache.commons.fileupload.FileItem)2 FileSizeLimitExceededException (org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException)2 FileUploadException (org.apache.commons.fileupload.FileUploadException)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)1 IOFileUploadException (org.apache.commons.fileupload.FileUploadBase.IOFileUploadException)1 InvalidContentTypeException (org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException)1 SizeLimitExceededException (org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1