java. Lang.numberformatexception: occurred for input string: ”

I have a problem deploying the application on the server (everything is OK on the local computer) In my application, users can upload files using multiupload This 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 an image on the server, I encounter 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: ""

We can't figure out what it means and how to solve it By the way, I noticed that when I upload a 100-200 KB file, everything is normal. When the file is 4-5 MB, I will encounter exceptions

Thank you in advance!

Solution

It seems that "story_id" is not always set; The correlation with file size may or may not be a coincidence

You should protect your code from such client errors by making the "story_id" parameter optional This is a good idea for all request parameters, because it can prevent the server from crashing due to incorrectly formed 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
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>