Java.lang.NumberFormatException: for input line: ""

I got a problem while deploying my application on the server side (on local machine everything works fine). In my application, user can use multiupload to upload files. Here is my controller:

@Controller
public class FileUploadController {

    @Autowired
    private StoryService storyService;

    @Autowired
    private PhotoService photoService;

    @RequestMapping("/uploader")
    public String home() {

        // will be resolved to /views/fileUploader.jsp
        return "admin/fileUploader";
    }

    @RequestMapping(value = "/admin/story/upload", method = RequestMethod.POST)
    public @ResponseBody
    String upload(MultipartHttpServletRequest request,
                              HttpServletResponse response, HttpServletRequest req) throws IOException {

        //get story id
        Integer story_id = Integer.valueOf(req.getParameter("story_id"));
        Story story = storyService.findById(story_id);

        // Getting uploaded files from the request object
        Map<String, MultipartFile> fileMap = request.getFileMap();

        // Iterate through the map
        for (MultipartFile multipartFile : fileMap.values()) {

            // Save the file to local disk
            String name = Long.toString(System.currentTimeMillis());

            //original size
            saveFileToLocalDisk(multipartFile, name + ".jpg");

            //medium size
            Thumbnails.of(convertMultifileToFile(multipartFile)).size(1800, 2400)
                    .toFile(new File(getDestinationLocation() + "medium_" + name));

            //thumbnail size
            Thumbnails.of(convertMultifileToFile(multipartFile)).size(600, 800)
                    .toFile(new File(getDestinationLocation() + "thumb_" + name));


            //Save to db
            savePhoto(multipartFile, name, story);
        }
        return "redirect:/admin";
    }

    private void saveFileToLocalDisk(MultipartFile multipartFile, String name)
            throws IOException, FileNotFoundException {

        FileCopyUtils.copy(multipartFile.getBytes(), new FileOutputStream(getDestinationLocation() +
                name));
    }

    private String getOutputFilename(MultipartFile multipartFile) {

        return getDestinationLocation() + multipartFile.getOriginalFilename();
    }

    private Photo savePhoto(MultipartFile multipartFile, String name, Story story)
            throws IOException {

        Photo photo = new Photo();
        if (story != null) {
            photo.setName(name);
            photo.setStory(story);
            photoService.addPhoto(photo);
        }
        return photo;
    }

    private String getDestinationLocation() {
        return "/var/www/static/images/";
    }

    public File convertMultifileToFile(MultipartFile file) throws IOException
    {
        File convFile = new File(file.getOriginalFilename());
        convFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(convFile);
        fos.write(file.getBytes());
        fos.close();
        return convFile;
    }
}

      

When I try to upload images to the server, I get the following exception:

SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NumberFormatException: For input string: ""] with root cause
java.lang.NumberFormatException: For input string: ""

      

Can't figure out what this means and how to solve it. BTW, I noticed that when I upload files that are 100-200KB in size, everything is fine, when the files are 4-5MB in size, I get an exception.

Thanks in advance!

+3


source to share


2 answers


It seems that it is "story_id"

not always asked; correlation with file size may or may not be a coincidence.

You have to protect your code from client side errors like this by treating the parameter "story_id"

as optional. It's a good idea for all query parameters, because it prevents your backend from crashing from malformed requests:



String storyIdStr = req.getParameter("story_id");
if (storyIdStr == null || storyIdStr.length() == 0) {
    // Deal with the error
}
Integer story_id = null;
try {
    story_id = Integer.valueOf(storyIdStr);
} catch (NumberFormatException nfe) {
    // Deal with the error
}

      

+4


source


Integer.valueOf(req.getParameter("story_id"));

will give you this exception if it req.getParameter("story_id")

returns an empty string, since an empty string cannot be parsed as Integer

.



+3


source







All Articles