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.sivalabs.jcart.admin.web.controllers.ProductController.java

protected void saveProductImageToDisk(ProductForm productForm) {
    MultipartFile file = productForm.getImage();
    if (nonNull(file) && !file.isEmpty()) {
        String name = new StringBuilder(IMAGES_DIR).append(productForm.getId()).append(".jpg").toString();
        try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)))) {
            byte[] bytes = file.getBytes();
            stream.write(bytes);//from ww  w  . j  a v  a2  s  . c  om
        } catch (IOException e) {
            throw new JCartException(e);
        }
    }
}

From source file:com.card.loop.xyz.pageControllers.LOIDEController.java

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void uploadLearningElement(@RequestParam("title") String title, @RequestParam("author") String authorID,
        @RequestParam("description") String description, @RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {/*  w w w  . j  a v a  2  s. c o  m*/
            byte[] bytes = file.getBytes();
            File fil = new File(AppConfig.UPLOAD_BASE_PATH + title);

            if (!fil.getParentFile().exists())
                fil.getParentFile().mkdirs();

            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fil));
            stream.write(bytes);
            stream.close();

            LearningElement le = new LearningElement();
            le.setTitle(title);
            le.setUploadedBy(authorID);
            le.setDescription(description);
            le.setDownloads(0);
            le.setStatus(1);
            le.setRating(1);
            le.setUploadDate(new Date());
            le.setFilePath(AppConfig.DOWNLOAD_BASE_PATH);
            le.setFilename(file.getOriginalFilename());
            le.setContentType(file.getOriginalFilename().split("\\.")[1]);
            daoLE.addLearningElement(le);

            //   LearningElement lo = new LearningElement(title, authorID, description);

            //   lo.setType(file.getOriginalFilename().split("\\.")[1]);
            //   Database.get().save(lo);

            System.out.println("UPLOAD FINISHED");
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        System.err.println("EMPTY FILE.");
    }
}

From source file:controllers.ColorController.java

@RequestMapping("/updateFromXml")
public String updateFromXml(Map<String, Object> model,
        @RequestParam(value = "xlsFile", required = false) MultipartFile file, RedirectAttributes ras) {
    if (file == null || file.isEmpty()) {
        ras.addFlashAttribute("error", "File not found");
    } else {//from   w ww.  jav  a2 s  .c om
        File newFile = new File("/usr/local/etc/Color");
        if (newFile.exists()) {
            newFile.delete();
        }
        try {
            FileUtils.writeByteArrayToFile(newFile, file.getBytes());
            colorService.updateFromXml(newFile);
            ras.addFlashAttribute("error", colorService.getResult().getErrors());
        } catch (Exception e) {
            ras.addFlashAttribute("error", "updateFromXml" + e.getMessage());
        }
        if (newFile.exists()) {
            newFile.delete();
        }
    }
    return "redirect:/Color/show";
}

From source file:com.example.securelogin.app.common.validation.UploadFileNotEmptyValidator.java

@Override
public boolean isValid(MultipartFile multipartFile, ConstraintValidatorContext context) {
    if (multipartFile == null || !StringUtils.hasLength(multipartFile.getOriginalFilename())) {
        return true;
    }/*from   w  ww .j ava 2  s . c  o  m*/
    return !multipartFile.isEmpty();
}

From source file:controllers.ColorGroupController.java

@RequestMapping("/updateFromXml")
public String updateFromXml(Map<String, Object> model,
        @RequestParam(value = "xlsFile", required = false) MultipartFile file, RedirectAttributes ras) {
    if (file == null || file.isEmpty()) {
        ras.addFlashAttribute("error", "File not found");
    } else {/* w  ww.java 2 s.c o  m*/
        File newFile = new File("/usr/local/etc/ColorGroup");
        if (newFile.exists()) {
            newFile.delete();
        }
        try {
            FileUtils.writeByteArrayToFile(newFile, file.getBytes());
            colorGroupService.updateFromXml(newFile);
            ras.addFlashAttribute("error", colorGroupService.getResult().getErrors());
        } catch (Exception e) {
            ras.addFlashAttribute("error", "updateFromXml" + e.getMessage());
        }
        if (newFile.exists()) {
            newFile.delete();
        }
    }
    return "redirect:/ColorGroup/show";
}

From source file:cn.cug.laboratory.controller.FileUpload.java

@RequestMapping(value = "/action/file.do", method = { RequestMethod.POST, RequestMethod.GET })
public @ResponseBody String uploadPhoto(@RequestParam MultipartFile uFile, HttpServletRequest request,
        HttpServletResponse response, ModelMap map) {
    System.out.println("Hello");
    try {/*from   w  w w . j  a  v  a 2s .co  m*/
        if (uFile != null && !uFile.isEmpty()) {
            System.out.println("file:" + uFile.getOriginalFilename());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "success";
}

From source file:controllers.CCOController.java

@RequestMapping("/updateFromXml")
public String updateFromXml(Map<String, Object> model,
        @RequestParam(value = "xlsFile", required = false) MultipartFile file, RedirectAttributes ras) {
    if (file == null || file.isEmpty()) {
        ras.addFlashAttribute("error", "File not found");
    } else {//from   ww  w. ja  va 2  s. c  om
        File newFile = new File("/usr/local/etc/CCO");
        if (newFile.exists()) {
            newFile.delete();
        }
        try {
            FileUtils.writeByteArrayToFile(newFile, file.getBytes());
            carCompletionOptionService.updateFromXml(newFile);
            ras.addFlashAttribute("error", carCompletionOptionService.getResult().getErrors());
        } catch (Exception e) {
            ras.addFlashAttribute("error", "updateFromXml" + e.getMessage());
        }
        if (newFile.exists()) {
            newFile.delete();
        }
    }
    return "redirect:/CCO/show";
}

From source file:csns.web.controller.UserControllerS.java

private void handleProfilePicture(User user, MultipartFile uploadedFile) {
    if (uploadedFile == null || uploadedFile.isEmpty())
        return;// ww w  .  j  a v a2  s  . c  o m

    File picture0 = fileIO.save(uploadedFile, user, true);
    File picture1 = imageUtils.resizeToProfilePicture(picture0);
    File picture2 = imageUtils.resizeToProfileThumbnail(picture0);
    user.setOriginalPicture(picture0);
    user.setProfilePicture(picture1);
    user.setProfileThumbnail(picture2);
    user = userDao.saveUser(user);

    logger.info("Saved profile picture for " + user.getUsername());
}

From source file:com.aboutdata.web.controller.member.SettingsController.java

/**
 * @1 ?//from   w  ww  .  j a v  a 2s  . com
 * @2 ??40x40 60x60 200x200
 * @3 Avatar ???? ??ids(?)?
 * @param multipartFile
 * @param model
 * @return
 */
@RequestMapping(value = "/avatar", method = RequestMethod.POST)
public String updateAvatar(MultipartFile multipartFile, ModelMap model) {
    if (!multipartFile.isEmpty()) {
        Member member = memberService.getCurrent();
        //?????
        imageGraphicsService.buildAvatar(member.getId(), multipartFile);

        String type = multipartFile.getContentType().split("/")[1];
        member.setAvatar(multipartFile.getOriginalFilename());
        member.setAvatarType(type);
        memberService.update(member);
    }
    return "redirect:/member/settings/avatar";
}

From source file:com.xiovr.unibot.web.controller.ManageController.java

@RequestMapping(value = "/bot/uploadscript", method = RequestMethod.POST)
public @ResponseBody String scriptFileUpload(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {/*  w  w  w.  ja  va2 s . co  m*/
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(
                    botGameConfig.getAbsDirPath() + "/" + botEnvironment.getScriptsPathPrefix() + "/" + name)));
            stream.write(bytes);
            stream.close();
            return "You successfully uploaded " + name + " into " + name + "-uploaded !";
        } catch (Exception e) {
            return "You failed to upload " + name + " => " + e.getMessage();
        }
    } else {
        return "You failed to upload " + name + " because the file was empty.";
    }
}