在之前只是进行了单一的文件上传,但是在很多的情况下是有可能要传递多个文件的,所以这个时候首先要解决的一个问题就是表单里面需要提供有多个上传的项,而后在控制器上再进行一些简单的处理
1.修改上传表单:
upload_page.html
SpringBoot模版渲染
2.修改控制器中的参数接收,这个地方需要一点点特殊处理
package cn.mldn.microboot.controller;import java.util.Iterator;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;import cn.mldn.microboot.util.controller.AbstractBaseController;@Controllerpublic class UploadController extends AbstractBaseController { @RequestMapping(value = "/uploadPre", method = RequestMethod.GET) public String uploadPre() { // 通过model可以实现内容的传递 return "upload_page"; } @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody public String upload(String name, HttpServletRequest request) { if (request instanceof MultipartHttpServletRequest) { // 如果你现在是MultipartHttpServletRequest对象 MultipartHttpServletRequest mrequest = (MultipartHttpServletRequest) request; Listfiles = mrequest.getFiles("photo"); Iterator iter = files.iterator(); while (iter.hasNext()) { MultipartFile photo = iter.next() ; if (photo != null) { // 现在有文件上传 System.out.println("【*** 文件上传 ****】name = " + name); System.out.println( "【*** 文件上传 ****】photoName = " + photo.getName()); System.out.println("【*** 文件上传 ****】photoContentType = " + photo.getContentType()); System.out.println( "【*** 文件上传 ****】photoSize = " + photo.getSize()); } } } return "upload-file"; }}
多个上传文件的信息要通过spring包裹的request对象获得。