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:sg.ncl.MainController.java

@RequestMapping(path = "/show_pub_keys", method = RequestMethod.POST)
public String addPublicKey(@RequestParam("keyFile") MultipartFile keyFile,
        @RequestParam("keyPass") String keyPass, RedirectAttributes redirectAttributes, HttpSession session)
        throws WebServiceRuntimeException {
    if (keyFile.isEmpty()) {
        redirectAttributes.addFlashAttribute(MESSAGE, "Please select a keyfile to upload");
        redirectAttributes.addFlashAttribute("hasKeyFileError", true);
    } else if (keyPass.isEmpty()) {
        redirectAttributes.addFlashAttribute(MESSAGE, "Please enter your password");
        redirectAttributes.addFlashAttribute("hasKeyPassError", true);
    } else {/*from w  w w  . j  av  a 2 s  .c  o  m*/
        try {
            JSONObject keyInfo = new JSONObject();
            keyInfo.put("publicKey", new String(keyFile.getBytes()));
            keyInfo.put(PSWD, keyPass);
            HttpEntity<String> request = createHttpEntityWithBody(keyInfo.toString());
            restTemplate.setErrorHandler(new MyResponseErrorHandler());
            ResponseEntity response = restTemplate.exchange(
                    properties.getPublicKeys(session.getAttribute("id").toString()), HttpMethod.POST, request,
                    String.class);
            String responseBody = response.getBody().toString();

            if (RestUtil.isError(response.getStatusCode())) {
                MyErrorResource error = objectMapper.readValue(responseBody, MyErrorResource.class);
                ExceptionState exceptionState = ExceptionState.parseExceptionState(error.getError());
                switch (exceptionState) {
                case VERIFICATION_PASSWORD_NOT_MATCH_EXCEPTION:
                    log.error(error.getMessage());
                    redirectAttributes.addFlashAttribute(MESSAGE, "Invalid password");
                    redirectAttributes.addFlashAttribute("hasKeyPassError", true);
                    break;
                case INVALID_PUBLIC_KEY_FILE_EXCEPTION:
                    log.error(error.getMessage());
                    redirectAttributes.addFlashAttribute(MESSAGE, "Invalid key file");
                    break;
                case INVALID_PUBLIC_KEY_FORMAT_EXCEPTION:
                    log.error(error.getMessage());
                    redirectAttributes.addFlashAttribute(MESSAGE, "Invalid key format");
                    break;
                case FORBIDDEN_EXCEPTION:
                    log.error(error.getMessage());
                    redirectAttributes.addFlashAttribute(MESSAGE, "Adding of public key is forbidden");
                    break;
                default:
                    log.error("Unknown error when adding public key");
                    redirectAttributes.addFlashAttribute(MESSAGE, "Unknown error when adding public key");
                }
            }
        } catch (IOException e) {
            throw new WebServiceRuntimeException(e.getMessage());
        }
    }

    return "redirect:/show_pub_keys";
}

From source file:tds.websim.presentation.services.WebSimXHR.java

@RequestMapping(value = "LoadTestPackage")
@ResponseBody//  w  ww  .j  av a  2s .c  om
public ReturnStatus loadTestPackage(
        @RequestParam(value = "testPackage", required = false) MultipartFile testPackage,
        HttpServletResponse response) throws TDSSecurityException, ReturnStatusException {
    getUser();
    if (!_user.isAuth()) {
        throw new TDSSecurityException();
    }

    ReturnStatus ret = null;
    if (testPackage.isEmpty()) {
        throw new ReturnStatusException("File is empty");
    }

    try {
        InputStream in = testPackage.getInputStream();
        DocumentBuilderFactory documentBuildFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuildFactory.newDocumentBuilder();
        Document doc = documentBuilder.parse(in);
        doc.getDocumentElement().normalize();
        String testKey = getTestKey(doc);
        if (testKey == null)
            throw new ReturnStatusException("uniqueid / testkey cannot be found in the test package XML");
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        StringWriter sw = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(sw));
        String xmlTestPackage = sw.toString();
        ret = getIBLoadTask().loadConfig(testKey, xmlTestPackage);
        setStatus(response, HttpServletResponse.SC_OK);
    } catch (ReturnStatusException ex) {
        throw ex;
    } catch (Exception e) {
        throw new ReturnStatusException(e);
    }
    return ret;
}

From source file:ua.aits.Carpath.controller.ArchiveController.java

@RequestMapping(value = { "/system/archive/do/uploadfile", "/system/archive/do/uploadfile/",
        "/Carpath/system/archive/do/uploadfile",
        "/Carpath/system/archive/do/uploadfile/" }, method = RequestMethod.POST)
public @ResponseBody String uploadFileHandler(@RequestParam("file") MultipartFile file,
        @RequestParam("path") String path, HttpServletRequest request) {
    String name = file.getOriginalFilename();
    if (!file.isEmpty()) {
        try {//from  www .java2s  .  co  m
            byte[] bytes = file.getBytes();
            File dir = new File(Constants.HOME + path);
            if (!dir.exists())
                dir.mkdirs();
            File serverFile = new File(dir.getAbsolutePath() + File.separator + name);
            try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile))) {
                stream.write(bytes);
            }
            return "";
        } 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:ua.aits.Carpath.controller.ArchiveController.java

@RequestMapping(value = { "/system/archive/do/uploadimage", "/system/archive/do/uploadimage/",
        "/Carpath/system/archive/do/uploadimage",
        "/Carpath/system/archive/do/uploadimage/" }, method = RequestMethod.POST)
public @ResponseBody String uploadImageHandler(@RequestParam("file") MultipartFile file,
        @RequestParam("path") String path, HttpServletRequest request) {
    String name = file.getOriginalFilename();
    if (!file.isEmpty()) {
        try {//ww  w  .j a v  a 2 s  . c o  m
            byte[] bytes = file.getBytes();
            File dir = new File(Constants.HOME + path);
            if (!dir.exists())
                dir.mkdirs();
            File serverFile = new File(dir.getAbsolutePath() + File.separator + name);
            try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile))) {
                stream.write(bytes);
            }
            return "";
        } 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:ua.aits.Carpath.controller.FileUploadController.java

/**
 * Upload single file using Spring Controller
 * @param file//  www.  j a v a  2 s. c  o  m
 * @param path
 * @param request
 * @return 
 */
@RequestMapping(value = { "/uploadFile", "/Carpath/uploadFile", }, method = RequestMethod.POST)
public @ResponseBody String uploadFileHandler(@RequestParam("upload") MultipartFile file,
        @RequestParam("path") String path, HttpServletRequest request) {

    String name = file.getOriginalFilename();
    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();
            // Creating the directory to store file
            File dir = new File(Constants.HOME + path);
            if (!dir.exists())
                dir.mkdirs();

            // Create the file on server
            File serverFile = new File(dir.getAbsolutePath() + File.separator + name);
            try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile))) {
                stream.write(bytes);
            }
            String link_path = serverFile.getAbsolutePath().replace(Constants.HOME, "");
            return "<a href=\"#\" class=\"returnImage\" data-url='" + Constants.URL + path + name + "'>"
                    + "<img src=\"" + Constants.URL + link_path + "\" realpath='" + link_path + "'  alt='"
                    + link_path + file.getName() + "'  /><img src='" + Constants.URL
                    + "img/remove.png' class='remove-icon'/></a>";
        } 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:ua.aits.Carpath.controller.FileUploadController.java

@RequestMapping(value = { "/uploadIcon", "/Carpath/uploadIcon" }, method = RequestMethod.POST)
public @ResponseBody String uploadFileHandlerMarker(@RequestParam("upload") MultipartFile file,
        HttpServletRequest request) {/*from w  w w . j av  a  2  s  . c o m*/

    String name = file.getOriginalFilename();
    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();
            // Creating the directory to store file
            File dir = new File(Constants.FILE_URL_ICON);

            File serverFile = new File(dir.getAbsolutePath() + File.separator + name);
            try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile))) {
                stream.write(bytes);
            }
            return "<a class=\"returnImage\" data-url='" + Constants.URL + "img/markerImages/" + name + "'>"
                    + "<img src='" + Constants.URL + "img/content/" + name + "' alt='" + name
                    + "'  /><img src='" + Constants.URL + "img/remove.png' class='remove-icon'/></a>";
        } 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:ua.aits.Carpath.controller.FileUploadController.java

@RequestMapping(value = { "/system/uploadRoute", "/Carpath/system/uploadRoute" }, method = RequestMethod.POST)
public @ResponseBody String uploadFileHandlerRoute(@RequestParam("upload") MultipartFile file,
        HttpServletRequest request) {/*  w  ww .j a va  2  s. c  o  m*/

    String name = file.getOriginalFilename();
    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();
            // Creating the directory to store file
            File dir = new File(Constants.FILE_URL_ROUTES);

            File serverFile = new File(dir.getAbsolutePath() + File.separator + name);
            try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile))) {
                stream.write(bytes);
            }
            return 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:ua.aits.Carpath.controller.FileUploadController.java

@RequestMapping(value = { "/system/uploadPanorama",
        "/Carpath/system/uploadPanorama" }, method = RequestMethod.POST)
public @ResponseBody String uploadFileHandlerPanorama(@RequestParam("upload") MultipartFile file,
        HttpServletRequest request) {//from ww  w.  java2  s .c  om

    String name = file.getOriginalFilename();
    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();
            // Creating the directory to store file
            File dir = new File(Constants.HOME + "files/panoramas/");

            File serverFile = new File(dir.getAbsolutePath() + File.separator + name);
            try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile))) {
                stream.write(bytes);
            }
            return 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:ua.aits.Carpath.controller.SystemController.java

@RequestMapping(value = { "/system/user/do/insertdata.do",
        "/Carpath/system/user/do/insertdata.do" }, method = RequestMethod.POST)
public ModelAndView doAddUser(@RequestParam("user_avatar") MultipartFile file, HttpServletRequest request)
        throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException,
        UnsupportedEncodingException {
    request.setCharacterEncoding("UTF-8");
    String user_name = request.getParameter("user_name");
    String user_password = request.getParameter("user_password");
    String user_role = request.getParameter("user_role");
    String user_enabled = request.getParameter("user_enabled");
    String user_firstname = request.getParameter("user_firstname");
    String user_lastname = request.getParameter("user_lastname");
    String user_descr = request.getParameter("user_descr");
    String user_contacts = request.getParameter("user_contacts");
    String name = file.getOriginalFilename();
    String user_avatar = "";
    if (!file.isEmpty()) {
        try {//ww  w.j a  v a  2  s  .co m
            byte[] bytes = file.getBytes();
            File dir = new File(Constants.HOME + "user_avatars/");

            File serverFile = new File(dir.getAbsolutePath() + File.separator + user_name + "."
                    + FilenameUtils.getExtension(name));
            try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile))) {
                stream.write(bytes);
            }
            user_avatar = "user_avatars/" + user_name + "." + FilenameUtils.getExtension(name);
        } catch (Exception e) {
            System.out.println("You failed to upload " + name + " => " + e.getMessage());
        }
    } else {
        System.out.println("You failed to upload " + name + " because the file was empty.");
    }
    Users.addUser(user_name, user_password, user_firstname, user_lastname, user_contacts, user_role,
            user_enabled, user_descr, user_avatar);
    return new ModelAndView("redirect:" + "/system/users");
}

From source file:ua.aits.Carpath.controller.SystemController.java

@RequestMapping(value = { "/system/user/do/updatedata.do",
        "/Carpath/system/user/do/updatedata.do" }, method = RequestMethod.POST)
public ModelAndView doEditUser(@RequestParam("user_avatar") MultipartFile file, HttpServletRequest request)
        throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException,
        UnsupportedEncodingException {
    request.setCharacterEncoding("UTF-8");
    String user_id = request.getParameter("user_id");
    String user_name = request.getParameter("user_name");
    String user_password = request.getParameter("user_password");
    String user_role = request.getParameter("user_role");
    String user_enabled = request.getParameter("user_enabled");
    String user_firstname = request.getParameter("user_firstname");
    String user_lastname = request.getParameter("user_lastname");
    String user_descr = request.getParameter("user_descr");
    String user_contacts = request.getParameter("user_contacts");
    String name = file.getOriginalFilename();
    String user_avatar = request.getParameter("user_avatar_old");
    if (!file.isEmpty()) {
        try {//from   w  w w  .j a  v  a 2s . com
            byte[] bytes = file.getBytes();
            File dir = new File(Constants.HOME + "user_avatars/");

            File serverFile = new File(dir.getAbsolutePath() + File.separator + user_name + "."
                    + FilenameUtils.getExtension(name));
            try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile))) {
                stream.write(bytes);
            }

        } catch (Exception e) {
            System.out.println("You failed to upload " + name + " => " + e.getMessage());
        }
        user_avatar = "user_avatars/" + user_name + "." + FilenameUtils.getExtension(name);
    }
    if ("img/noavatar.png".equals(user_avatar)) {
        user_avatar = "";
    }
    Users.editUser(user_id, user_name, user_password, user_firstname, user_lastname, user_contacts, user_role,
            user_enabled, user_descr, user_avatar);
    return new ModelAndView("redirect:" + "/system/users");
}