java.lang.NumberFormatException:对于输入字符串:"&引用;发生
我在服务器端部署应用程序时遇到了一个问题(在本地机器上,一切正常)。在我的应用程序中,用户可以使用multiupload上载文件。这是我的控制器:java.lang.NumberFormatException:对于输入字符串:"&引用;发生,java,Java,我在服务器端部署应用程序时遇到了一个问题(在本地机器上,一切正常)。在我的应用程序中,用户可以使用multiupload上载文件。这是我的控制器: @Controller public class FileUploadController { @Autowired private StoryService storyService; @Autowired private PhotoService photoService; @RequestMappi
@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;
}
}
无法理解它的含义以及如何解决它。顺便说一句,我注意到当我上传100-200KB的文件时,一切正常,当文件大小为4-5MB时,我会遇到异常
提前谢谢 Integer.valueOf(req.getParameter(“story_id”))如果req.getParameter(“story\u id”)
返回一个空字符串,则code>将给出此异常,因为空字符串不能被解析为整数似乎并非总是设置“story\u id”
;与文件大小的关联可能是巧合,也可能不是巧合
您应该通过将“story\u 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
}
你在Java 8中使用过注释@notnull或类似的东西吗?@KickButtowski我不确定它在这里是否会有很大帮助,因为story\u id
似乎被设置为空字符串,而不是null
。我明白了,但你使用过我提到的注释吗?@KickButtowski没有,我从来没有使用过这个注释(我也从来没有在工作中使用过Java8)。我在看一个关于Java8的视频,有这样一个注释,但是我没有找到任何关于它的有用信息。你应该看看
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
}