@RequestMapping(value = "/image.action") public String upload( @RequestParam(value ="image", required = false) MultipartFile file, @RequestParam(value ="image1", required = false) MultipartFile file1, HttpServletRequest request, ModelMap model) { String path = request.getSession().getServletContext().getRealPath("/") + "upfiles/"; String fileName = file.getOriginalFilename(); int i = fileName.lastIndexOf("."); String name = String.valueOf(VeDate.getStringDatex()); String type = fileName.substring(i + 1); fileName = name + "." + type; File targetFile = new File(path, fileName); if (!targetFile.exists()) { targetFile.mkdirs(); } // 保存第一张图片 try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } // 保存第二张图片 String fileName1 = file1.getOriginalFilename(); int j = fileName1.lastIndexOf("."); String name1 = String.valueOf(VeDate.getStringDatex()); String type1 = fileName1.substring(j + 1); fileName1 = name1 + "." + type1; File targetFile1 = new File(path, fileName1); if (!targetFile1.exists()) { targetFile1.mkdirs(); } try { file1.transferTo(targetFile1); } catch (Exception e) { e.printStackTrace(); } model.addAttribute("imageFileName", fileName); model.addAttribute("image1FileName", fileName1); return "page/saveimage"; }如何空指针异常错误,列出具体方法
时间: 2023-06-15 19:07:34 浏览: 104
在该代码中,可能会出现空指针异常的方法有:
1. file.getOriginalFilename():如果file为null,则会抛出空指针异常。
2. file1.getOriginalFilename():如果file1为null,则会抛出空指针异常。
3. targetFile.mkdirs():如果创建目录失败,则会返回null,而如果在返回值上调用方法,会导致空指针异常。
4. file.transferTo(targetFile):如果file为null或者targetFile为null,则会抛出空指针异常。
5. file1.transferTo(targetFile1):如果file1为null或者targetFile1为null,则会抛出空指针异常。
6. model.addAttribute("imageFileName", fileName):如果model为null,则会抛出空指针异常。
7. model.addAttribute("image1FileName", fileName1):如果model为null,则会抛出空指针异常。
因此,在代码中应该加上空指针异常的判断,避免程序运行出现异常。
相关问题
@RequestMapping(value = "/image.action") //public String upload(@RequestParam(value ="image", required = false) MultipartFile file, HttpServletRequest request, ModelMap model) { public String upload( @RequestParam(value ="image", required = false) MultipartFile file, @RequestParam(value ="image1", required = false) MultipartFile file1, HttpServletRequest request, ModelMap model) { String path = request.getSession().getServletContext().getRealPath("/") + "upfiles/"; String fileName = file.getOriginalFilename(); int i = fileName.lastIndexOf("."); String name = String.valueOf(VeDate.getStringDatex()); String type = fileName.substring(i + 1); fileName = name + "." + type; File targetFile = new File(path, fileName); if (!targetFile.exists()) { targetFile.mkdirs(); } // 保存 try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } model.addAttribute("imageFileName", fileName); return "page/saveimage"; }如何使image和image1有同样的效果
你可以将参数名改为相同的名称,这样在接收时就可以共用一个方法了,示例代码如下:
```java
@RequestMapping(value = "/image.action")
public String upload(@RequestParam(value = "image", required = false) MultipartFile image,
@RequestParam(value = "image", required = false) MultipartFile image1,
HttpServletRequest request, ModelMap model) {
String path = request.getSession().getServletContext().getRealPath("/") + "upfiles/";
// 以下省略文件上传和保存的代码
model.addAttribute("imageFileName", fileName);
return "page/saveimage";
}
```
@RequestMapping(value = "/files.action") public String files( @RequestParam(value ="image", required = false) MultipartFile file, @RequestParam(value ="image1", required = false) MultipartFile file1, HttpServletRequest request, ModelMap model) { String path = request.getSession().getServletContext().getRealPath("/") + "upfiles/"; String fileName = file.getOriginalFilename(); int i = fileName.lastIndexOf("."); String name = String.valueOf(VeDate.getStringDatex()); String type = fileName.substring(i + 1); fileName = name + "." + type; File targetFile = new File(path, fileName); if (!targetFile.exists()) { targetFile.mkdirs(); } // 保存 try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } model.addAttribute("imageFileName", fileName); return "page/savefile"; }如何使参数image和image1有一样的效果
可以将@RequestParam(value ="image", required = false) MultipartFile file和@RequestParam(value ="image1", required = false) MultipartFile file1合并成一个@RequestParam(value ="file", required = false) MultipartFile[] files,然后在方法内部通过循环处理每一个文件。修改后的代码如下:
@RequestMapping(value = "/files.action")
public String files(@RequestParam(value ="file", required = false) MultipartFile[] files, HttpServletRequest request, ModelMap model) {
String path = request.getSession().getServletContext().getRealPath("/") + "upfiles/";
List<String> fileNames = new ArrayList<String>();
for(MultipartFile file: files){
String fileName = file.getOriginalFilename();
int i = fileName.lastIndexOf(".");
String name = String.valueOf(VeDate.getStringDatex());
String type = fileName.substring(i + 1);
fileName = name + "." + type;
File targetFile = new File(path, fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
fileNames.add(fileName);
}
model.addAttribute("imageFileNames", fileNames);
return "page/savefile";
}
阅读全文