Search in sources :

Example 76 with FileUploadException

use of org.apache.commons.fileupload.FileUploadException 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)

Example 77 with FileUploadException

use of org.apache.commons.fileupload.FileUploadException in project flow by vaadin.

the class StreamReceiverHandler method handleMultipartFileUploadFromInputStream.

private boolean handleMultipartFileUploadFromInputStream(VaadinSession session, VaadinRequest request, StreamReceiver streamReceiver, StateNode owner) throws IOException {
    boolean success = true;
    long contentLength = getContentLength(request);
    // Parse the request
    FileItemIterator iter;
    try {
        iter = getItemIterator(request);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            boolean itemSuccess = handleStream(session, streamReceiver, owner, contentLength, item);
            success = success && itemSuccess;
        }
    } catch (FileUploadException e) {
        success = false;
        getLogger().warn("File upload failed.", e);
    }
    return success;
}
Also used : FileItemStream(org.apache.commons.fileupload.FileItemStream) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 78 with FileUploadException

use of org.apache.commons.fileupload.FileUploadException in project application by collectionspace.

the class WebUIRequest method initRequest.

private void initRequest(UIUmbrella umbrella, HttpServletRequest request, HttpServletResponse response, List<String> p) throws IOException, UIException {
    this.request = request;
    this.response = response;
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload();
        // Parse the request
        FileItemIterator iter;
        try {
            iter = upload.getItemIterator(request);
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                String name = item.getFieldName();
                // InputStream stream = item.openStream();
                if (item.isFormField()) {
                // System.out.println("Form field " + name + " with value "
                // + Streams.asString(stream) + " detected.");
                } else {
                    // System.out.println("File field " + name + " with file name "
                    // + item.getName() + " detected.");
                    // Process the input stream
                    contentHeaders = item.getHeaders();
                    uploadName = item.getName();
                    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                    if (item != null) {
                        InputStream stream = item.openStream();
                        IOUtils.copy(stream, byteOut);
                        new TeeInputStream(stream, byteOut);
                    }
                    bytebody = byteOut.toByteArray();
                }
            }
        } catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        body = IOUtils.toString(request.getInputStream(), "UTF-8");
    }
    this.ppath = p.toArray(new String[0]);
    if (!(umbrella instanceof WebUIUmbrella))
        throw new UIException("Bad umbrella");
    this.umbrella = (WebUIUmbrella) umbrella;
    session = calculateSessionId();
}
Also used : FileItemStream(org.apache.commons.fileupload.FileItemStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) InputStream(java.io.InputStream) UIException(org.collectionspace.csp.api.ui.UIException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 79 with FileUploadException

use of org.apache.commons.fileupload.FileUploadException in project tutorials by eugenp.

the class UploadController method handleUpload.

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleUpload(HttpServletRequest request) {
    System.out.println(System.getProperty("java.io.tmpdir"));
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    // Create a factory for disk-based file items
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
    factory.setSizeThreshold(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD);
    factory.setFileCleaningTracker(null);
    // Configure a repository (to ensure a secure temp location is used)
    ServletFileUpload upload = new ServletFileUpload(factory);
    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()) {
                try (InputStream uploadedStream = item.getInputStream();
                    OutputStream out = new FileOutputStream("file.mov")) {
                    IOUtils.copy(uploadedStream, out);
                    out.close();
                }
            }
        }
        // Parse the request with Streaming API
        upload = new ServletFileUpload();
        FileItemIterator iterStream = upload.getItemIterator(request);
        while (iterStream.hasNext()) {
            FileItemStream item = iterStream.next();
            String name = item.getFieldName();
            InputStream stream = item.openStream();
            if (!item.isFormField()) {
            // Process the InputStream
            } else {
                // process form fields
                String formFieldValue = Streams.asString(stream);
            }
        }
        return "success!";
    } catch (IOException | FileUploadException ex) {
        return "failed: " + ex.getMessage();
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 80 with FileUploadException

use of org.apache.commons.fileupload.FileUploadException in project wechat by dllwh.

the class UploadUtils method initFields.

/**
 * @Title:initFields
 * @Description:处理上传内容
 * @param request
 * @return:void 返回类型
 */
@SuppressWarnings("unchecked")
private static Map<String, Object> initFields(HttpServletRequest request) {
    // 存储表单字段和非表单字段
    Map<String, Object> resultMap = new HashMap<String, Object>();
    // 第一步:判断request
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    // 第二步:解析request
    if (isMultipart) {
        // Create a factory for disk-based file items
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // 阀值,超过这个值才会写到临时目录,否则在内存中
        factory.setSizeThreshold(1024 * 1024 * 10);
        factory.setRepository(new File(tempPath));
        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("UTF-8");
        // 最大上传限制
        upload.setSizeMax(maxSize);
        /* FileItem */
        List<FileItem> items = null;
        // Parse the request
        try {
            items = upload.parseRequest(request);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        // 第3步:处理uploaded items
        if (items != null && items.size() > 0) {
            Iterator<FileItem> iter = items.iterator();
            // 文件域对象
            List<FileItem> list = new ArrayList<FileItem>();
            // 表单字段
            Map<String, String> fields = new HashMap<String, String>();
            while (iter.hasNext()) {
                FileItem item = iter.next();
                // 处理所有表单元素和文件域表单元素
                if (item.isFormField()) {
                    // 表单元素
                    String name = item.getFieldName();
                    String value = item.getString();
                    fields.put(name, value);
                } else {
                    // 文件域表单元素
                    list.add(item);
                }
            }
            resultMap.put(FORM_FIELDS, fields);
            resultMap.put(FILE_FIELDS, list);
        }
    }
    return resultMap;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) File(java.io.File) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

FileUploadException (org.apache.commons.fileupload.FileUploadException)88 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)65 FileItem (org.apache.commons.fileupload.FileItem)57 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)51 IOException (java.io.IOException)37 HashMap (java.util.HashMap)27 ArrayList (java.util.ArrayList)22 List (java.util.List)21 File (java.io.File)20 InputStream (java.io.InputStream)19 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)19 FileItemStream (org.apache.commons.fileupload.FileItemStream)19 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)16 ServletException (javax.servlet.ServletException)12 Map (java.util.Map)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 Iterator (java.util.Iterator)8 ApplicationContext (org.springframework.context.ApplicationContext)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)6