Search in sources :

Example 6 with ServletFileUpload

use of org.apache.commons.fileupload.servlet.ServletFileUpload in project ninja by ninjaframework.

the class NinjaServletContext method getFileItemIterator.

@Override
public FileItemIterator getFileItemIterator() {
    long maxFileSize = ninjaProperties.getIntegerWithDefault(NinjaConstant.UPLOADS_MAX_FILE_SIZE, -1);
    long maxTotalSize = ninjaProperties.getIntegerWithDefault(NinjaConstant.UPLOADS_MAX_TOTAL_SIZE, -1);
    ServletFileUpload upload = new ServletFileUpload();
    upload.setFileSizeMax(maxFileSize);
    upload.setSizeMax(maxTotalSize);
    FileItemIterator fileItemIterator = null;
    try {
        fileItemIterator = upload.getItemIterator(httpServletRequest);
    } catch (FileUploadException | IOException e) {
        logger.error("Error while trying to process mulitpart file upload", e);
    }
    return fileItemIterator;
}
Also used : ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) IOException(java.io.IOException) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 7 with ServletFileUpload

use of org.apache.commons.fileupload.servlet.ServletFileUpload 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 8 with ServletFileUpload

use of org.apache.commons.fileupload.servlet.ServletFileUpload in project v7files by thiloplanz.

the class BucketsServlet method doFormPost.

private void doFormPost(HttpServletRequest request, HttpServletResponse response, BSONObject bucket) throws IOException {
    ObjectId uploadId = new ObjectId();
    BSONObject parameters = new BasicBSONObject();
    List<FileItem> files = new ArrayList<FileItem>();
    if (ServletFileUpload.isMultipartContent(request)) {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            for (Object _file : upload.parseRequest(request)) {
                FileItem file = (FileItem) _file;
                if (file.isFormField()) {
                    String v = file.getString();
                    parameters.put(file.getFieldName(), v);
                } else {
                    files.add(file);
                }
            }
        } catch (FileUploadException e) {
            throw new IOException(e);
        }
    } else {
        for (Entry<String, String[]> param : request.getParameterMap().entrySet()) {
            String[] v = param.getValue();
            if (v.length == 1)
                parameters.put(param.getKey(), v[0]);
            else
                parameters.put(param.getKey(), v);
        }
    }
    BSONObject result = new BasicBSONObject("_id", uploadId);
    BSONObject uploads = new BasicBSONObject();
    for (FileItem file : files) {
        DiskFileItem f = (DiskFileItem) file;
        // inline until 10KB
        if (f.isInMemory()) {
            uploads.put(f.getFieldName(), storage.inlineOrInsertContentsAndBackRefs(10240, f.get(), uploadId, f.getName(), f.getContentType()));
        } else {
            uploads.put(f.getFieldName(), storage.inlineOrInsertContentsAndBackRefs(10240, f.getStoreLocation(), uploadId, f.getName(), f.getContentType()));
        }
        file.delete();
    }
    result.put("files", uploads);
    result.put("parameters", parameters);
    bucketCollection.update(new BasicDBObject("_id", bucket.get("_id")), new BasicDBObject("$push", new BasicDBObject("FormPost.data", result)));
    String redirect = BSONUtils.getString(bucket, "FormPost.redirect");
    // redirect mode
    if (StringUtils.isNotBlank(redirect)) {
        response.sendRedirect(redirect + "?v7_formpost_id=" + uploadId);
        return;
    }
    // echo mode
    // JSON does not work, see https://jira.mongodb.org/browse/JAVA-332
    // response.setContentType("application/json");
    // response.getWriter().write(JSON.serialize(result));
    byte[] bson = BSON.encode(result);
    response.getOutputStream().write(bson);
}
Also used : ObjectId(org.bson.types.ObjectId) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) BasicBSONObject(org.bson.BasicBSONObject) FileItem(org.apache.commons.fileupload.FileItem) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem) BasicDBObject(com.mongodb.BasicDBObject) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) FileUploadException(org.apache.commons.fileupload.FileUploadException) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem)

Example 9 with ServletFileUpload

use of org.apache.commons.fileupload.servlet.ServletFileUpload in project jaggery by wso2.

the class RequestHostObject method parseMultipart.

private static void parseMultipart(RequestHostObject rho) throws ScriptException {
    if (rho.files != null) {
        return;
    }
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List items = null;
    try {
        items = upload.parseRequest(rho.request);
    } catch (FileUploadException e) {
        log.error(e.getMessage(), e);
        throw new ScriptException(e);
    }
    // Process the uploaded items
    String name;
    rho.files = rho.context.newObject(rho);
    for (Object obj : items) {
        FileItem item = (FileItem) obj;
        name = item.getFieldName();
        if (item.isFormField()) {
            ArrayList<FileItem> x = (ArrayList<FileItem>) rho.parameterMap.get(name);
            if (x == null) {
                ArrayList<FileItem> array = new ArrayList<FileItem>(1);
                array.add(item);
                rho.parameterMap.put(name, array);
            } else {
                x.add(item);
            }
        } else {
            rho.files.put(item.getFieldName(), rho.files, rho.context.newObject(rho, "File", new Object[] { item }));
        }
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ScriptableObject(org.mozilla.javascript.ScriptableObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 10 with ServletFileUpload

use of org.apache.commons.fileupload.servlet.ServletFileUpload in project symmetric-ds by JumpMind.

the class FileSyncPushUriHandler method handle.

public void handle(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException, FileUploadException {
    String nodeId = ServletUtils.getParameter(req, WebConstants.NODE_ID);
    if (StringUtils.isBlank(nodeId)) {
        ServletUtils.sendError(res, HttpServletResponse.SC_BAD_REQUEST, "Node must be specified");
        return;
    } else if (!ServletFileUpload.isMultipartContent(req)) {
        ServletUtils.sendError(res, HttpServletResponse.SC_BAD_REQUEST, "We only handle multipart requests");
        return;
    } else {
        log.debug("File sync push request received from {}", nodeId);
    }
    ServletFileUpload upload = new ServletFileUpload();
    // Parse the request
    FileItemIterator iter = upload.getItemIterator(req);
    while (iter.hasNext()) {
        FileItemStream item = iter.next();
        String name = item.getFieldName();
        if (!item.isFormField()) {
            log.debug("Processing upload file field " + name + " with file name " + item.getName() + " detected.");
            engine.getFileSyncService().loadFilesFromPush(nodeId, item.openStream(), res.getOutputStream());
        }
    }
    res.flushBuffer();
}
Also used : ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) FileItemIterator(org.apache.commons.fileupload.FileItemIterator)

Aggregations

ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)43 FileItem (org.apache.commons.fileupload.FileItem)29 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)29 FileUploadException (org.apache.commons.fileupload.FileUploadException)20 File (java.io.File)17 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)13 InputStream (java.io.InputStream)7 HashMap (java.util.HashMap)7 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)7 FileItemStream (org.apache.commons.fileupload.FileItemStream)7 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)6 ServletException (javax.servlet.ServletException)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 List (java.util.List)4 Locale (java.util.Locale)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)4 DataBinder (org.springframework.validation.DataBinder)4 Errors (org.springframework.validation.Errors)4