@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都能实现文件上传
时间: 2023-06-16 16:05:02 浏览: 167
可以在方法的参数中加入另一个MultipartFile类型的参数,并在@RequestParam注解中指定参数名为"image1",如下所示:
```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/";
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";
}
```
这样,在上传时就可以同时上传两张图片,分别保存为fileName和fileName1。
阅读全文