Example usage for org.springframework.web.multipart MultipartRequest getMultiFileMap

List of usage examples for org.springframework.web.multipart MultipartRequest getMultiFileMap

Introduction

In this page you can find the example usage for org.springframework.web.multipart MultipartRequest getMultiFileMap.

Prototype

MultiValueMap<String, MultipartFile> getMultiFileMap();

Source Link

Document

Return a MultiValueMap of the multipart files contained in this request.

Usage

From source file:com.mycompany.testdowload.controller.DocumentFileController.java

@RequestMapping(value = "/savefile", method = RequestMethod.POST)
@ResponseBody/*w  ww . j  a va 2s .c o  m*/
public void saveFile(MultipartRequest file) throws IOException {
    System.out.println("----------------------------------->" + file.getMultiFileMap());
    UploadFile uploadFile = new UploadFile();
    uploadFile.setName(file.getFile("file").getOriginalFilename());
    uploadFile.setMimeType(file.getFile("file").getContentType());
    uploadFile.setContent(file.getFile("file").getBytes());
    uploadFileRepo.save(uploadFile);
}

From source file:com.mycompany.uploadfile.controller.StudentImageController.java

@RequestMapping(value = "/savefile", method = RequestMethod.POST)
//     @RequestMapping(value = "/savefile", method = RequestMethod.POST)
//    @ResponseBody
public void saveImage(MultipartRequest file) throws IOException {
    System.out// w w w.  j a v  a2  s  .c om
            .println("------------------------------------------------------------->" + file.getMultiFileMap());
    System.out.println("------------------------------------------------------------->" + file.getFile("file"));
    StudentImage image = new StudentImage();
    image.setContent(file.getFile("file").getBytes());
    image.setName(file.getFile("file").getOriginalFilename());
    image.setMimeType(file.getFile("file").getName());
    imageRepo.save(image);
}

From source file:net.paoding.rose.web.paramresolver.ServletRequestDataBinder.java

/**
 * Bind the parameters of the given request to this binder's target, also
 * binding multipart files in case of a multipart request.
 * <p>/*from  w ww .ja  v a 2  s .  co  m*/
 * This call can create field errors, representing basic binding errors like
 * a required field (code "required"), or type mismatch between value and
 * bean property (code "typeMismatch").
 * <p>
 * Multipart files are bound via their parameter name, just like normal HTTP
 * parameters: i.e. "uploadedFile" to an "uploadedFile" bean property,
 * invoking a "setUploadedFile" setter method.
 * <p>
 * The type of the target property for a multipart file can be
 * MultipartFile, byte[], or String. The latter two receive the contents of
 * the uploaded file; all metadata like original file name, content type,
 * etc are lost in those cases.
 * 
 * @param request
 *            request with parameters to bind (can be multipart)
 * @see org.springframework.web.multipart.MultipartHttpServletRequest
 * @see org.springframework.web.multipart.MultipartFile
 * @see #bindMultipartFiles
 * @see #bind(org.springframework.beans.PropertyValues)
 */
public void bind(ServletRequest request) {
    MutablePropertyValues mpvs = new MutablePropertyValues(WebUtils.getParametersStartingWith(request, prefix));
    MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);
    if (multipartRequest != null) {
        bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
    }
    addBindValues(mpvs, request);
    doBind(mpvs);
}

From source file:org.broadleafcommerce.openadmin.server.service.artifact.upload.UploadAddOrUpdateController.java

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    Map<String, String> model = new HashMap<String, String>();
    String callbackName = null;/*from  ww  w .  j a  v a2 s  . c  o m*/
    try {
        MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);
        if (request instanceof MultipartRequest) {
            MultipartRequest multipartRequest = (MultipartRequest) request;
            bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
        }

        //check for XSRF
        String csrfToken = (String) mpvs.getPropertyValue("csrfToken").getValue();
        exploitProtectionService.compareToken(csrfToken);

        PersistencePackage persistencePackage = new PersistencePackage();
        persistencePackage.setPersistencePerspective(new PersistencePerspective());
        persistencePackage.setCsrfToken(csrfToken);
        String ceilingEntity = (String) mpvs.getPropertyValue("ceilingEntityFullyQualifiedClassname")
                .getValue();
        callbackName = (String) mpvs.getPropertyValue("callbackName").getValue();
        String operation = (String) mpvs.getPropertyValue("operation").getValue();
        String customCriteria = (String) mpvs.getPropertyValue("customCriteria").getValue();
        mpvs.removePropertyValue("ceilingEntityFullyQualifiedClassname");
        mpvs.removePropertyValue("sandbox");
        mpvs.removePropertyValue("callbackName");
        mpvs.removePropertyValue("operation");
        mpvs.removePropertyValue("customCriteria");
        persistencePackage.setCeilingEntityFullyQualifiedClassname(ceilingEntity);
        persistencePackage.setCustomCriteria(new String[] { customCriteria });
        Entity entity = new Entity();
        persistencePackage.setEntity(entity);
        entity.setType(new String[] { ceilingEntity });
        List<Property> propertyList = new ArrayList<Property>();
        for (PropertyValue propertyValue : mpvs.getPropertyValues()) {
            if (propertyValue.getValue() instanceof MultipartFile) {
                MultipartFile file = (MultipartFile) propertyValue.getValue();
                if (file.getSize() > maximumFileSizeInBytes) {
                    throw new MaxUploadSizeExceededException(maximumFileSizeInBytes);
                }
                if (file.getOriginalFilename() == null || file.getOriginalFilename().indexOf(".") < 0) {
                    throw new FileExtensionUnavailableException(
                            "Unable to determine file extension for uploaded file. The filename for the uploaded file is: "
                                    + file.getOriginalFilename());
                }
                Map<String, MultipartFile> fileMap = UploadedFile.getUpload();
                fileMap.put(propertyValue.getName(), (MultipartFile) propertyValue.getValue());
                UploadedFile.setUpload(fileMap);
                entity.setMultiPartAvailableOnThread(true);
            } else {
                Property property = new Property();
                property.setName(propertyValue.getName());
                property.setValue((String) propertyValue.getValue());
                propertyList.add(property);
            }
        }
        entity.setProperties(propertyList.toArray(new Property[] {}));

        Entity result = null;

        if (operation.equals("add")) {
            result = dynamicEntityRemoteService.add(persistencePackage);
        } else if (operation.equals("update")) {
            result = dynamicEntityRemoteService.update(persistencePackage);
        }

        model.put("callbackName", callbackName);
        model.put("result", buildJSON(result));

        return new ModelAndView("blUploadCompletedView", model);
    } catch (MaxUploadSizeExceededException e) {
        if (callbackName != null) {
            model.put("callbackName", callbackName);
            model.put("error", buildErrorJSON(e.getMessage()));
        }

        return new ModelAndView("blUploadCompletedView", model);
    } catch (FileExtensionUnavailableException e) {
        if (callbackName != null) {
            model.put("callbackName", callbackName);
            model.put("error", buildErrorJSON(e.getMessage()));
        }

        return new ModelAndView("blUploadCompletedView", model);
    } catch (Exception e) {
        e.printStackTrace();
        if (callbackName != null) {
            model.put("callbackName", callbackName);
            model.put("error", buildErrorJSON(e.getMessage()));
        }

        return new ModelAndView("blUploadCompletedView", model);
    } finally {
        UploadedFile.remove();
    }
}