Search in sources :

Example 1 with FileItem

use of ninja.uploads.FileItem in project ninja by ninjaframework.

the class NinjaServletContext method processFormFields.

private void processFormFields() {
    if (formFieldsProcessed)
        return;
    formFieldsProcessed = true;
    // return if not multipart
    if (!ServletFileUpload.isMultipartContent(httpServletRequest))
        return;
    // get fileProvider from route method/class, or defaults to an injected one
    // if none injected, then we do not process form fields this way and let the user
    // call classic getFileItemIterator() by himself
    FileProvider fileProvider = null;
    if (route != null) {
        if (fileProvider == null) {
            fileProvider = route.getControllerMethod().getAnnotation(FileProvider.class);
        }
        if (fileProvider == null) {
            fileProvider = route.getControllerClass().getAnnotation(FileProvider.class);
        }
    }
    // get file item provider from file provider or default one
    FileItemProvider fileItemProvider = null;
    if (fileProvider == null) {
        fileItemProvider = injector.getInstance(FileItemProvider.class);
    } else {
        fileItemProvider = injector.getInstance(fileProvider.value());
    }
    if (fileItemProvider instanceof NoFileItemProvider)
        return;
    // Initialize maps and other constants
    ArrayListMultimap<String, String> formMap = ArrayListMultimap.create();
    ArrayListMultimap<String, FileItem> fileMap = ArrayListMultimap.create();
    // This is the iterator we can use to iterate over the contents of the request.
    try {
        FileItemIterator fileItemIterator = getFileItemIterator();
        while (fileItemIterator.hasNext()) {
            FileItemStream item = fileItemIterator.next();
            if (item.isFormField()) {
                String charset = NinjaConstant.UTF_8;
                String contentType = item.getContentType();
                if (contentType != null) {
                    charset = HttpHeaderUtils.getCharsetOfContentTypeOrUtf8(contentType);
                }
                // save the form field for later use from getParameter
                String value = Streams.asString(item.openStream(), charset);
                formMap.put(item.getFieldName(), value);
            } else {
                // process file as input stream and save for later use in getParameterAsFile or getParameterAsInputStream
                FileItem fileItem = fileItemProvider.create(item);
                fileMap.put(item.getFieldName(), fileItem);
            }
        }
    } catch (FileUploadException | IOException e) {
        throw new RuntimeException("Failed to parse multipart request data", e);
    }
    // convert both multimap<K,V> to map<K,List<V>>
    formFieldsMap = toUnmodifiableMap(formMap);
    fileFieldsMap = toUnmodifiableMap(fileMap);
}
Also used : NoFileItemProvider(ninja.uploads.NoFileItemProvider) FileItemProvider(ninja.uploads.FileItemProvider) NoFileItemProvider(ninja.uploads.NoFileItemProvider) IOException(java.io.IOException) FileItem(ninja.uploads.FileItem) FileItemStream(org.apache.commons.fileupload.FileItemStream) FileProvider(ninja.uploads.FileProvider) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 2 with FileItem

use of ninja.uploads.FileItem in project ninja by ninjaframework.

the class UploadControllerAuto method uploadFinishAuto.

/**
     * 
     * This upload method expects a file and simply displays the file in the
     * multipart upload again to the user (in the correct mime encoding).
     * 
     * @param context
     * @return
     * @throws Exception
     */
@FileProvider(DiskFileItemProvider.class)
public Result uploadFinishAuto(Context context, @Param("file") File file, @Params("file") File[] files, @Param("file") InputStream inputStream, @Params("file") InputStream[] inputStreams, @Param("file") FileItem fileItem, @Params("file") FileItem[] fileItems, @Param("file2") File file2) throws Exception {
    StringBuilder sb = new StringBuilder();
    // file
    sb.append("file\n").append(IOUtils.toString(new FileInputStream(file))).append("\n");
    // files
    for (File f : files) {
        sb.append("files\n").append(IOUtils.toString(new FileInputStream(f))).append("\n");
    }
    // file inputstream
    sb.append("inputstream\n").append(IOUtils.toString(inputStream)).append("\n");
    // files inputstream
    for (InputStream is : inputStreams) {
        sb.append("inputstreams\n").append(IOUtils.toString(is)).append("\n");
    }
    // file fileItem
    sb.append("fileitem\n").append(IOUtils.toString(fileItem.getInputStream())).append("\n");
    // file fileItems
    for (FileItem fi : fileItems) {
        sb.append("fileitems\n").append(IOUtils.toString(fi.getInputStream())).append("\n");
    }
    // context.getParameterAsFileItem
    sb.append("getParameterAsFileItem\n").append(IOUtils.toString(context.getParameterAsFileItem("file").getInputStream())).append("\n");
    // file2
    sb.append("file2\n").append(IOUtils.toString(new FileInputStream(file2))).append("\n");
    return Results.ok().renderRaw(sb.toString().getBytes());
}
Also used : FileItem(ninja.uploads.FileItem) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) FileProvider(ninja.uploads.FileProvider)

Aggregations

FileItem (ninja.uploads.FileItem)2 FileProvider (ninja.uploads.FileProvider)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 FileItemProvider (ninja.uploads.FileItemProvider)1 NoFileItemProvider (ninja.uploads.NoFileItemProvider)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1