当我在服务器端部署我的应用程序时(在本地机器上一切正常),我正面临一个问题。在我的应用程序中,用户可以使用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时,我会得到异常。
提前致谢!
你有注解@NotNull或类似的在Java中8什么工作? – 2014-10-28 16:12:48
@KickButtowski我不知道这里是否会有很多帮助,因为'story_id'似乎被设置为一个空字符串,而不是'null'。 – dasblinkenlight 2014-10-28 16:19:00
我明白了,但是你是否使用了我提到的注释? – 2014-10-28 16:19:48