简体   繁体   English

java.lang.NumberFormatException:对于输入字符串:“”发生

[英]java.lang.NumberFormatException: For input string: “” occured

I'm facing an issue when I deploy my application on server side (on local machine everything works fine). 我在服务器端部署应用程序时遇到问题(在本地计算机上一切正常)。 In my application, a user can use multiupload for uploading files. 在我的应用程序中,用户可以使用multiupload上传文件。 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 on the server, I'm getting 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 it means and how to solve it. 无法弄清楚它意味着什么以及如何解决它。 By the way, I've noticed that when I upload files which are 100-200 KB everything is ok, when files are 4-5 MB I get exception. 顺便说一句,我注意到当我上传100-200 KB的文件时一切正常,当文件为4-5 MB时,我会遇到异常。

Thanks in advance! 提前致谢!

It appears that "story_id" is not always set; 似乎"story_id"并不总是设定; correlation with the file size may or may not be a coincidence. 与文件大小的相关可能是也可能不是巧合。

You should protect your code from client-side errors like this by treating the "story_id" parameter as optional. 您应该通过将"story_id"参数视为可选项来保护您的代码免受此类客户端错误的影响。 This is a good idea for all request parameters, because it prevents your server side from crashing on improperly 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
}

Integer.valueOf(req.getParameter("story_id")); will give you this exception if req.getParameter("story_id") returns an empty String, since an empty String can't be parsed as an Integer . 如果req.getParameter("story_id")返回一个空字符串,则会给出此异常,因为无法将空字符串解析为Integer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM