chapter_009_Spring_MVC/src/main/java/my/mvc/presentation/UploadServlet.java
@RequestMapping(value = "/upload", method = RequestMethod.POST)
    public
    @ResponseBody
    String uploadMultipleFileHandler(@RequestParam("itemId") String itemId,
                                     @RequestParam("file") MultipartFile[] files) {
        String message = "File upload";
        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            try {
                byte[] bytes = file.getBytes();
                String appPath = context.getRealPath("");
                // save file in directory
                File serverFile = new File(appPath
                        + File.separator
                        + SAVE_DIR
                        + File.separator
                        + generateUniqueFileName(itemId)
                        + "."
                        + FilenameUtils.getExtension(file.getOriginalFilename()));

                FileUtils.writeByteArrayToFile(serverFile, file.getBytes());
                // save file in db
                FileImage image = new FileImage();
                image.setName(serverFile.getName());
                image.setPath(FilenameUtils.getFullPathNoEndSeparator(serverFile.getPath()));
                image.setItemId(Integer.valueOf(itemId));
                LOGIC.add(image);

                logger.info("Server File Location="
                        + serverFile.getAbsolutePath());

            } catch (Exception e) {
                return "You failed to upload " + itemId + " => " + e.getMessage();
            }
        }
        return message;
    }
/**
     * Upload multiple file using Spring Controller
     */
Upload multiple file using Spring Controller
Upload multiple file using Spring Controller
@RequestMapping(value = "/upload", method = RequestMethod.POST)
    public
    @ResponseBody
    String uploadMultipleFileHandler(@RequestParam("itemId") String itemId,
                                     @RequestParam("file") MultipartFile[] files) {
        String message = "File upload";
        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            try {
                byte[] bytes = file.getBytes();
                String appPath = context.getRealPath("");
                File serverFile = new File(appPath
                        + File.separator
                        + SAVE_DIR
                        + File.separator
                        + generateUniqueFileName(itemId)
                        + "."
                        + FilenameUtils.getExtension(file.getOriginalFilename()));
                FileUtils.writeByteArrayToFile(serverFile, file.getBytes());
                FileImage image = new FileImage();
                image.setName(serverFile.getName());
                image.setPath(FilenameUtils.getFullPathNoEndSeparator(serverFile.getPath()));
                image.setItemId(Integer.valueOf(itemId));
                LOGIC.add(image);
                logger.info("Server File Location="
                        + serverFile.getAbsolutePath());
            } catch (Exception e) {
                return "You failed to upload " + itemId + " => " + e.getMessage();
            }
        }
        return message;
    }
@ RequestMapping ( value = "/upload" , method = RequestMethod . POST ) public @ ResponseBody String uploadMultipleFileHandler ( @ RequestParam ( "itemId" ) String itemId , @ RequestParam ( "file" ) MultipartFile [ ] files ) { String message = "File upload" ; for ( int i = 0 ; i < files . length ; i ++ ) { MultipartFile file = files [ i ] ; try { byte [ ] bytes = file . getBytes ( ) ; String appPath = context . getRealPath ( "" ) ; File serverFile = new File ( appPath + File . separator + SAVE_DIR + File . separator + generateUniqueFileName ( itemId ) + "." + FilenameUtils . getExtension ( file . getOriginalFilename ( ) ) ) ; FileUtils . writeByteArrayToFile ( serverFile , file . getBytes ( ) ) ; FileImage image = new FileImage ( ) ; image . setName ( serverFile . getName ( ) ) ; image . setPath ( FilenameUtils . getFullPathNoEndSeparator ( serverFile . getPath ( ) ) ) ; image . setItemId ( Integer . valueOf ( itemId ) ) ; LOGIC . add ( image ) ; logger . info ( "Server File Location=" + serverFile . getAbsolutePath ( ) ) ; } catch ( Exception e ) { return "You failed to upload " + itemId + " => " + e . getMessage ( ) ; } } return message ; }
Upload multiple file using Spring Controller
Upload multiple file using Spring Controller
// save file in directory // save file in db