Example usage for org.springframework.web.multipart MaxUploadSizeExceededException MaxUploadSizeExceededException

List of usage examples for org.springframework.web.multipart MaxUploadSizeExceededException MaxUploadSizeExceededException

Introduction

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

Prototype

public MaxUploadSizeExceededException(long maxUploadSize) 

Source Link

Document

Constructor for MaxUploadSizeExceededException.

Usage

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   www .ja v a 2s  . com
    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();
    }
}

From source file:org.esupportail.publisher.service.FileService.java

private String uploadResource(final Long entityId, final String name, final MultipartFile file,
        final FileUploadHelper fileUploadHelper) throws MultipartException {
    // Checking size
    if (file.getSize() > fileUploadHelper.getFileMaxSize()) {
        throw new MaxUploadSizeExceededException(fileUploadHelper.getFileMaxSize());
    }/*from  w ww.  j  av  a 2 s .c  om*/
    // Checking ContentType
    Pair<Boolean, MultipartException> isAuthorized = isAuthorizedMimeType(file, fileUploadHelper);
    Assert.notNull(isAuthorized.getFirst());
    if (!isAuthorized.getFirst()) {
        if (isAuthorized.getSecond() != null)
            throw isAuthorized.getSecond();
        return null;
    }

    try {
        final String fileExt = Files.getFileExtension(file.getOriginalFilename()).toLowerCase();
        String fname = new SimpleDateFormat("yyyyMMddHHmmss'." + fileExt + "'").format(new Date());
        //            if (name != null && name.length() > 0 ) {
        //                fname = Files.getNameWithoutExtension(name) + "-" + fname;
        //            }
        final String internPath = fileUploadHelper.getUploadDirectoryPath();
        final String relativPath = String.valueOf(entityId).hashCode() + File.separator + df.format(new Date())
                + File.separator;
        final String path = internPath + relativPath;
        File inFile = new File(path + fname);
        // checking if path is existing
        if (!inFile.getParentFile().exists()) {
            boolean error = !inFile.getParentFile().mkdirs();
            if (error) {
                log.error("Can't create directory {} to upload file, track error!",
                        inFile.getParentFile().getPath());
                return null;
            }
        }
        // checking if file exist else renaming file name
        int i = 1;
        while (inFile.exists()) {
            inFile = new File(path + i + fname);
            i++;
        }
        log.debug("Uploading file as {}", inFile.getPath());
        // start upload
        file.transferTo(inFile);
        return fileUploadHelper.getUrlResourceMapping() + relativPath + fname;
    } catch (Exception e) {
        log.error("File Upload error", e);
        return null;
    }
}