Search in sources :

Example 26 with ServletFileUpload

use of org.apache.commons.fileupload.servlet.ServletFileUpload in project eweb4j-framework by laiweiwei.

the class UploadUtil method handleUpload.

public static void handleUpload(Context context) throws Exception {
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    UploadConfigBean ucb = cb.getMvc().getUpload();
    String tmpDir = ucb.getTmp();
    int memoryMax = CommonUtil.strToInt(CommonUtil.parseFileSize(ucb.getMaxMemorySize()) + "");
    long sizeMax = CommonUtil.parseFileSize(ucb.getMaxRequestSize());
    if (tmpDir.trim().length() == 0)
        tmpDir = "${RootPath}" + File.separator + "WEB-INF" + File.separator + "tmp";
    tmpDir = tmpDir.replace("${RootPath}", ConfigConstant.ROOT_PATH);
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(memoryMax);
    factory.setRepository(new File(tmpDir));
    ServletFileUpload _upload = new ServletFileUpload(factory);
    if (!_upload.isMultipartContent(context.getRequest()))
        return;
    _upload.setSizeMax(sizeMax);
    try {
        List<FileItem> items = _upload.parseRequest(context.getRequest());
        Iterator<FileItem> it = items.iterator();
        while (it.hasNext()) {
            FileItem item = it.next();
            String fieldName = item.getFieldName();
            if (item.isFormField()) {
                String value = item.getString();
                context.getQueryParamMap().put(fieldName, new String[] { value });
            } else {
                String fileName = item.getName();
                if (fileName == null || fileName.trim().length() == 0)
                    continue;
                String stamp = CommonUtil.getNowTime("yyyyMMddHHmmss");
                File tmpFile = new File(tmpDir + File.separator + stamp + "_" + fileName);
                item.write(tmpFile);
                UploadFile uploadFile = new UploadFile(tmpFile, fileName, fieldName, item.getSize(), item.getContentType());
                if (context.getUploadMap().containsKey(fieldName)) {
                    context.getUploadMap().get(fieldName).add(uploadFile);
                } else {
                    List<UploadFile> uploads = new ArrayList<UploadFile>();
                    uploads.add(uploadFile);
                    context.getUploadMap().put(fieldName, uploads);
                }
            }
        }
    } catch (InvalidContentTypeException e) {
        throw new Exception("upload file error", e);
    }
}
Also used : InvalidContentTypeException(org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException) ArrayList(java.util.ArrayList) UploadConfigBean(org.eweb4j.config.bean.UploadConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) InvalidContentTypeException(org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) File(java.io.File) UploadConfigBean(org.eweb4j.config.bean.UploadConfigBean)

Example 27 with ServletFileUpload

use of org.apache.commons.fileupload.servlet.ServletFileUpload in project JessMA by ldcsaa.

the class FileUploader method getFileUploadComponent.

private ServletFileUpload getFileUploadComponent() {
    DiskFileItemFactory dif = new DiskFileItemFactory();
    if (factorySizeThreshold != DEFAULT_SIZE_THRESHOLD)
        dif.setSizeThreshold(factorySizeThreshold);
    if (factoryRepository != null)
        dif.setRepository(new File(factoryRepository));
    if (factoryCleaningTracker != null)
        dif.setFileCleaningTracker(factoryCleaningTracker);
    ServletFileUpload sfu = new ServletFileUpload(dif);
    if (sizeMax != NO_LIMIT_SIZE_MAX)
        sfu.setSizeMax(sizeMax);
    if (fileSizeMax != NO_LIMIT_FILE_SIZE_MAX)
        sfu.setFileSizeMax(fileSizeMax);
    if (servletHeaderencoding != null)
        sfu.setHeaderEncoding(servletHeaderencoding);
    if (servletProgressListener != null)
        sfu.setProgressListener(servletProgressListener);
    return sfu;
}
Also used : ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) File(java.io.File)

Example 28 with ServletFileUpload

use of org.apache.commons.fileupload.servlet.ServletFileUpload in project zuul by Netflix.

the class FilterScriptManagerServlet method handlePostBody.

private String handlePostBody(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    org.apache.commons.fileupload.FileItemIterator it = null;
    try {
        it = upload.getItemIterator(request);
        while (it.hasNext()) {
            FileItemStream stream = it.next();
            InputStream input = stream.openStream();
            // NOTE: we are going to pull the entire stream into memory
            // this will NOT work if we have huge scripts, but we expect these to be measured in KBs, not MBs or larger
            byte[] uploadedBytes = getBytesFromInputStream(input);
            input.close();
            if (uploadedBytes.length == 0) {
                setUsageError(400, "ERROR: Body contained no data.", response);
                return null;
            }
            return new String(uploadedBytes);
        }
    } catch (FileUploadException e) {
        throw new IOException(e.getMessage());
    }
    return null;
}
Also used : ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) InputStream(java.io.InputStream) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) 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 29 with ServletFileUpload

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

the class WLMDataController method uploadManifest.

public ModelAndView uploadManifest(HttpServletRequest req, HttpServletResponse res) {
    log.info("WLMDataController.uploadManifest >>");
    req.getSession().removeAttribute("manifestValue");
    ModelAndView responseView = new ModelAndView(new JSONView());
    List<Map<String, String>> manifestValue = new ArrayList<Map<String, String>>();
    // Check that we have a file upload request
    boolean isMultipart = ServletFileUpload.isMultipartContent(req);
    System.out.println(isMultipart);
    if (!isMultipart) {
        responseView.addObject("result", false);
        return responseView;
    }
    // Create a factory for disk-based file items
    FileItemFactory factory = new DiskFileItemFactory();
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    // Parse the request
    try {
        @SuppressWarnings("unchecked") List<FileItem> items = upload.parseRequest(req);
        // Process the uploaded items
        Iterator<FileItem> iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
            if (!item.isFormField()) {
                String[] lines = item.getString().split("\\r?\\n");
                for (String values : lines) {
                    if (values.length() > 2) {
                        String[] val = values.split(":");
                        if (val.length == 2) {
                            Map<String, String> manifest = new HashMap<String, String>();
                            manifest.put(val[0], val[1]);
                            manifestValue.add(manifest);
                        } else {
                            responseView.addObject("result", false);
                            return responseView;
                        }
                    }
                }
            }
        }
        log.info("Uploaded Content :: " + manifestValue.toString());
        req.getSession().setAttribute("manifestValue", manifestValue);
        /*responseView.addObject("manifestValue",manifestValue);*/
        responseView.addObject("result", manifestValue.size() > 0 ? true : false);
    } catch (FileUploadException e) {
        e.printStackTrace();
        responseView.addObject("result", false);
    } catch (Exception e) {
        e.printStackTrace();
        responseView.addObject("result", false);
    }
    log.info("WLMDataController.uploadManifest <<<");
    return responseView;
}
Also used : HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ArrayList(java.util.ArrayList) 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) WLMPortalException(com.intel.mountwilson.common.WLMPortalException) FileItem(org.apache.commons.fileupload.FileItem) JSONView(com.intel.mountwilson.util.JSONView) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) HashMap(java.util.HashMap) Map(java.util.Map) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 30 with ServletFileUpload

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

the class ServletUtils method handleFileUploads.

// TODO: seriously, javadoc the piece handleFileUploads method
/**
	 * @param modelManager
	 * @param request
	 * @param map
	 * @return
	 * @throws FileUploadException
	 */
public static final Boolean handleFileUploads(Integer maxFileUploadSize, String fileStoragePrefix, HttpServletRequest request, Map<Object, Object> map) throws FileUploadException {
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(4096);
    factory.setRepository(new File(fileStoragePrefix));
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setSizeMax(maxFileUploadSize);
    List fileItems = upload.parseRequest(request);
    for (FileItem item : (List<FileItem>) fileItems) {
        // we'll put this in the parameter map,
        // assuming the backing that is looking for it
        // knows what to expect
        String fieldName = item.getFieldName();
        Boolean isFormField = item.isFormField();
        Long size = item.getSize();
        if (isFormField) {
            if (size > 0) {
                map.put(fieldName, new String[] { item.getString() });
            }
        } else if (!isFormField) {
            map.put(fieldName, item);
        }
    }
    return true;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) List(java.util.List) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) File(java.io.File)

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