2014-10-28 97 views
3

當我在服務器端部署我的應用程序時(在本地機器上一切正常),我正面臨一個問題。在我的應用程序中,用戶可以使用multiupload來上傳文件。這裏是我的控制器:java.lang.NumberFormatException:對於輸入字符串:「」發生

@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; 
    } 
} 

當我嘗試在服務器上上傳圖片,我發現了以下異常:

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: "" 

想不通這意味着什麼,如何解決這個問題。順便說一下,我注意到當我上傳100-200 KB的文件時,一切正常,當文件爲4-5 MB時,我會得到異常。

提前致謝!

回答

4

看來,"story_id"並不總是設置;與文件大小的關聯可能是也可能不是巧合。

您應該通過將"story_id"參數視爲可選參數來保護您的代碼不受此類客戶端錯誤的影響。這是所有請求參數是一個好主意,因爲它可以防止您的服務器端崩潰的格式不正確的請求:

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 
} 
+0

你有註解@NotNull或類似的在Java中8什麼工作? – 2014-10-28 16:12:48

+0

@KickButtowski我不知道這裏是否會有很多幫助,因爲'story_id'似乎被設置爲一個空字符串,而不是'null'。 – dasblinkenlight 2014-10-28 16:19:00

+0

我明白了,但是你是否使用了我提到的註釋? – 2014-10-28 16:19:48

3

Integer.valueOf(req.getParameter("story_id"));會給你這個例外,如果req.getParameter("story_id")返回一個空字符串,因爲空字符串不能被解析爲Integer

相關問題