@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/"; // 判断第一张图片是否为空 if (file == null) { throw new NullPointerException("第一张图片不能为空!"); } 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(); } // 保存第二张图片 // 判断第二张图片是否为空 if (file1 != null && !file1.isEmpty()) { 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("image1FileName", fileName1); } model.addAttribute("imageFileName", fileName); return "page/saveimage"; }上传第二张图片时会覆盖第一张图片文本框,该如何改写代码
时间: 2023-06-15 21:06:12 浏览: 152
可以将第一张图片和第二张图片的处理分开,分别判断是否存在并保存即可。以下是修改后的代码示例:
```java
@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/";
// 保存第一张图片
if (file != null && !file.isEmpty()) {
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);
}
// 保存第二张图片
if (file1 != null && !file1.isEmpty()) {
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("image1FileName", fileName1);
}
return "page/saveimage";
}
```
这样修改后,即使上传第二张图片时也不会覆盖第一张图片了。
阅读全文