@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-15 19:08:22 浏览: 73
你可以将参数名改为相同的名称,这样在接收时就可以共用一个方法了,示例代码如下:
```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";
}
@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和image1都放进去,如下所示:
```
@RequestParam(value = {"image", "image1"}, required = false) MultipartFile[] files
```
这样就可以同时接收两个参数,且效果相同。在方法内部,你需要遍历这个数组,然后分别处理每个文件。
阅读全文