Search in sources :

Example 1 with FileUpload

use of org.apache.commons.fileupload.FileUpload in project spring-framework by spring-projects.

the class CommonsMultipartResolver method parseRequest.

/**
	 * Parse the given servlet request, resolving its multipart elements.
	 * @param request the request to parse
	 * @return the parsing result
	 * @throws MultipartException if multipart resolution failed.
	 */
protected MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {
    String encoding = determineEncoding(request);
    FileUpload fileUpload = prepareFileUpload(encoding);
    try {
        List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
        return parseFileItems(fileItems, encoding);
    } catch (FileUploadBase.SizeLimitExceededException ex) {
        throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
    } catch (FileUploadBase.FileSizeLimitExceededException ex) {
        throw new MaxUploadSizeExceededException(fileUpload.getFileSizeMax(), ex);
    } catch (FileUploadException ex) {
        throw new MultipartException("Failed to parse multipart servlet request", ex);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) MaxUploadSizeExceededException(org.springframework.web.multipart.MaxUploadSizeExceededException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileUploadBase(org.apache.commons.fileupload.FileUploadBase) MultipartException(org.springframework.web.multipart.MultipartException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileUpload(org.apache.commons.fileupload.FileUpload) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 2 with FileUpload

use of org.apache.commons.fileupload.FileUpload in project spring-framework by spring-projects.

the class CommonsFileUploadSupport method prepareFileUpload.

/**
	 * Determine an appropriate FileUpload instance for the given encoding.
	 * <p>Default implementation returns the shared FileUpload instance
	 * if the encoding matches, else creates a new FileUpload instance
	 * with the same configuration other than the desired encoding.
	 * @param encoding the character encoding to use
	 * @return an appropriate FileUpload instance.
	 */
protected FileUpload prepareFileUpload(String encoding) {
    FileUpload fileUpload = getFileUpload();
    FileUpload actualFileUpload = fileUpload;
    // its own encoding that does not match the default encoding.
    if (encoding != null && !encoding.equals(fileUpload.getHeaderEncoding())) {
        actualFileUpload = newFileUpload(getFileItemFactory());
        actualFileUpload.setSizeMax(fileUpload.getSizeMax());
        actualFileUpload.setFileSizeMax(fileUpload.getFileSizeMax());
        actualFileUpload.setHeaderEncoding(encoding);
    }
    return actualFileUpload;
}
Also used : FileUpload(org.apache.commons.fileupload.FileUpload)

Example 3 with FileUpload

use of org.apache.commons.fileupload.FileUpload in project xwiki-platform by xwiki.

the class FileUploadPlugin method loadFileList.

/**
 * Loads the list of uploaded files in the context if there are any uploaded files.
 *
 * @param uploadMaxSize Maximum size of the uploaded files.
 * @param uploadSizeThreashold Threashold over which the file data should be stored on disk, and not in memory.
 * @param tempdir Temporary directory to store the uploaded files that are not kept in memory.
 * @param context Context of the request.
 * @throws XWikiException if the request could not be parsed, or the maximum file size was reached.
 * @see FileUploadPluginApi#loadFileList(long, int, String)
 */
public void loadFileList(long uploadMaxSize, int uploadSizeThreashold, String tempdir, XWikiContext context) throws XWikiException {
    LOGGER.debug("Loading uploaded files");
    // Continuing would empty the list.. We need to stop.
    if (context.get(FILE_LIST_KEY) != null) {
        LOGGER.debug("Called loadFileList twice");
        return;
    }
    // Get the FileUpload Data
    // Make sure the factory only ever creates file items which will be deleted when the jvm is stopped.
    DiskFileItemFactory factory = new DiskFileItemFactory() {

        @Override
        public FileItem createItem(String fieldName, String contentType, boolean isFormField, String fileName) {
            try {
                final DiskFileItem item = (DiskFileItem) super.createItem(fieldName, contentType, isFormField, fileName);
                // Needed to make sure the File object is created.
                item.getOutputStream();
                return item;
            } catch (IOException e) {
                String path = System.getProperty("java.io.tmpdir");
                if (super.getRepository() != null) {
                    path = super.getRepository().getPath();
                }
                throw new RuntimeException("Unable to create a temporary file for saving the attachment. " + "Do you have write access on " + path + "?");
            }
        }
    };
    factory.setSizeThreshold(uploadSizeThreashold);
    if (tempdir != null) {
        File tempdirFile = new File(tempdir);
        if (tempdirFile.mkdirs() && tempdirFile.canWrite()) {
            factory.setRepository(tempdirFile);
        }
    }
    // TODO: Does this work in portlet mode, or we must use PortletFileUpload?
    FileUpload fileupload = new ServletFileUpload(factory);
    RequestContext reqContext = new ServletRequestContext(context.getRequest().getHttpServletRequest());
    fileupload.setSizeMax(uploadMaxSize);
    try {
        @SuppressWarnings("unchecked") List<FileItem> list = fileupload.parseRequest(reqContext);
        if (list.size() > 0) {
            LOGGER.info("Loaded " + list.size() + " uploaded files");
        }
        // We store the file list in the context
        context.put(FILE_LIST_KEY, list);
    } catch (FileUploadBase.SizeLimitExceededException e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_FILE_EXCEPTION_MAXSIZE, "Exception uploaded file");
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_UPLOAD_PARSE_EXCEPTION, "Exception while parsing uploaded file", e);
    }
}
Also used : ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) XWikiException(com.xpn.xwiki.XWikiException) IOException(java.io.IOException) FileItem(org.apache.commons.fileupload.FileItem) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileUploadBase(org.apache.commons.fileupload.FileUploadBase) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) RequestContext(org.apache.commons.fileupload.RequestContext) File(java.io.File) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileUpload(org.apache.commons.fileupload.FileUpload) XWikiException(com.xpn.xwiki.XWikiException) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem)

Example 4 with FileUpload

use of org.apache.commons.fileupload.FileUpload in project camunda-bpm-platform by camunda.

the class MultipartPayloadProvider method readFrom.

public MultipartFormData readFrom(Class<MultipartFormData> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    final MultipartFormData multipartFormData = createMultipartFormDataInstance();
    final FileUpload fileUpload = createFileUploadInstance();
    String contentType = httpHeaders.getFirst("content-type");
    RestMultipartRequestContext requestContext = createRequestContext(entityStream, contentType);
    // parse the request (populates the multipartFormData)
    parseRequest(multipartFormData, fileUpload, requestContext);
    return multipartFormData;
}
Also used : FileUpload(org.apache.commons.fileupload.FileUpload)

Aggregations

FileUpload (org.apache.commons.fileupload.FileUpload)4 FileItem (org.apache.commons.fileupload.FileItem)2 FileUploadBase (org.apache.commons.fileupload.FileUploadBase)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 XWikiException (com.xpn.xwiki.XWikiException)1 File (java.io.File)1 IOException (java.io.IOException)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 RequestContext (org.apache.commons.fileupload.RequestContext)1 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ServletRequestContext (org.apache.commons.fileupload.servlet.ServletRequestContext)1 MaxUploadSizeExceededException (org.springframework.web.multipart.MaxUploadSizeExceededException)1 MultipartException (org.springframework.web.multipart.MultipartException)1