Example usage for org.springframework.web.multipart MultipartFile isEmpty

List of usage examples for org.springframework.web.multipart MultipartFile isEmpty

Introduction

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

Prototype

boolean isEmpty();

Source Link

Document

Return whether the uploaded file is empty, that is, either no file has been chosen in the multipart form or the chosen file has no content.

Usage

From source file:com.trenako.web.images.MultipartFileValidator.java

@Override
public void validate(Object target, Errors errors) {
    // skip validation for empty files.
    if (target == null)
        return;/*from  ww w .  j  a va 2 s .  co  m*/

    MultipartFile file = (MultipartFile) target;
    if (file.isEmpty())
        return;

    // validate file size
    if (file.getSize() > AppGlobals.MAX_UPLOAD_SIZE) {
        errors.reject("uploadFile.size.range.notmet", "Invalid media size. Max size is 512 Kb");
    }

    // validate content type
    MediaType contentType = MediaType.parseMediaType(file.getContentType());
    if (!AppGlobals.ALLOWED_MEDIA_TYPES.contains(contentType.toString())) {
        errors.reject("uploadFile.contentType.notvalid", "Invalid media type");
    }
}

From source file:com.slience.controller.PictureOfMongoStoreController.java

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {/*from ww w . ja  v a 2 s  . co  m*/
            //byte[] bytes = file.getBytes();
            //BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
            //stream.write(bytes);
            //stream.close();

            System.out.println("------------------------>" + file.getName() + ":" + file.getSize());

            return "You successfully uploaded " + name + "!";
        } catch (Exception e) {
            return "You failed to upload " + name + " => " + e.getMessage();
        }
    } else {
        return "You failed to upload " + name + " because the file was empty.";
    }
}

From source file:com.github.wxiaoqi.oss.controller.OssController.java

/**
 * /*from  w  w  w .ja va2  s  .c  o  m*/
 */
@RequestMapping("/upload")
public ObjectRestResponse<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
    if (file.isEmpty()) {
        throw new BaseException("?");
    }
    //
    String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
    String url = ossFactory.build().uploadSuffix(file.getBytes(), suffix);
    return new ObjectRestResponse<>().data(url);
}

From source file:io.isoft.reg.controller.FileUploadController.java

/**
 * Accepts a POST request with multipart/form-data content
 * @param description the name of the file being uploaded
 * @param file the binary file/*  ww w. j av  a  2s  . co  m*/
 * @return response message indicating success or failure
 */
@RequestMapping(value = "postformdata", method = RequestMethod.POST, headers = "Content-Type=multipart/form-data")
public @ResponseBody String handleFormUpload(@RequestParam("description") String description,
        @RequestParam("file") MultipartFile file) {

    if (!file.isEmpty()) {
        byte[] bytes = null;
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            logger.info("error processing uploaded file", e);
        }
        return "file upload received! Name:[" + description + "] Size:[" + bytes.length + "]";
    } else {
        return "file upload failed!";
    }
}

From source file:org.jtalks.common.web.validation.ImageSizeValidator.java

/**
 * Check that file's size no more imageSize.
 *
 * @param multipartFile image that user want upload as avatar
 * @param context       validation context
 * @return {@code true} if validation successfull or false if fails
 *//*from w w  w .j  a  v a2s  . c om*/
@Override
public boolean isValid(MultipartFile multipartFile, ConstraintValidatorContext context) {
    if (multipartFile.isEmpty()) {
        //assume that empty multipart file is valid to avoid validation message when user doesn't load nothing
        return true;
    }
    return multipartFile.getSize() / BYTES_IN_KILOBYTE < imageSize;
}

From source file:org.jtalks.common.web.validation.ImageFormatValidator.java

/**
 * Validate object with {@link ImageFormat} annotation.
 * Check that file has extension jpg, png or gif
 *
 * @param multipartFile image that user want upload as avatar
 * @param context       validation context
 * @return {@code true} if validation successfull or false if fails
 *//*from   w  w  w . j  a  va 2s .c om*/
@Override
public boolean isValid(MultipartFile multipartFile, ConstraintValidatorContext context) {
    if (multipartFile.isEmpty()) {
        //assume that empty multipart file is valid to avoid validation message when user doesn't load nothing
        return true;
    }
    String contentType = multipartFile.getContentType();
    for (ImageFormats format : formats) {
        if (format.getContentType().equals(contentType)) {
            return true;
        }
    }
    return false;
}

From source file:com.anthony.forumspring.controller.FileUploadController.java

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody String uploadFildHander(@RequestParam(value = "name") String name,
        @RequestParam("file") MultipartFile file) {

    if (!file.isEmpty()) {
        try {/*  w  w w  . j  a  v a  2  s .co m*/
            byte[] bytes = file.getBytes();

            // Creating the directory to store file
            String rootPath = System.getProperty("catalina.home");
            File dir = new File(rootPath + File.separator + "tmpFiles");
            if (!dir.exists()) {
                dir.mkdirs();
            }

            // Create the file on server
            File serverFile = new File(dir.getAbsolutePath() + File.separator + name);
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();

            //logger.info("Server File Location="
            //      + serverFile.getAbsolutePath());
            return "You successfully uploaded file=" + name;
        } catch (Exception e) {
            return "You failed to upload " + name + " => " + e.getMessage();
        }
    } else {
        return "You failed to upload " + name + " because the file was empty.";
    }
}

From source file:org.openmrs.module.hl7valuegroups.web.controller.HL7ValueGroupsManageController.java

@RequestMapping(method = RequestMethod.POST)
public void apply(@RequestParam("hl7") MultipartFile hl7) {
    if (hl7 == null || hl7.isEmpty())
        return;//from  ww  w  . jav  a  2 s . c o  m

    log.warn("processing hl7 ...");
    HL7Service hl7Service = Context.getHL7Service();

    HL7InQueue queue = new HL7InQueue();
    queue.setHL7Source(hl7Service.getHL7Source(1));
    try {
        queue.setHL7Data(new String(hl7.getBytes()));
    } catch (IOException e) {
        log.warn("could not create hl7 from this data");
        return;
    }

    hl7Service.saveHL7InQueue(queue);
    log.warn("hl7 saved ...");
}

From source file:cs544.videohouse.validator.ImageConstraintValidator.java

@Override
public boolean isValid(MultipartFile image, ConstraintValidatorContext context) {
    boolean valid = true;
    if (image.isEmpty()) {
        valid = false;//w w w .j ava 2  s . co  m
    } else {
        String imageExt = FilenameUtils.getExtension(image.getOriginalFilename());
        if (!"png".equals(imageExt) && !"jpg".equals(imageExt) && !"jpeg".equals(imageExt)
                && !"bmp".equals(imageExt)) {
            valid = false;
        }
    }
    return valid;
}

From source file:br.edu.unidavi.restapp.FileUploadController.java

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {/*from  w  w  w  .  j  a v a 2  s  . co  m*/
            String name = file.getOriginalFilename();
            System.out.println(name);
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
            stream.write(bytes);
            stream.close();
            return "You successfully uploaded !";
        } catch (Exception e) {
            return "You failed to upload => " + e.getMessage();
        }
    } else {
        return "You failed to upload because the file was empty.";
    }
}