defaultEncoding: 必须和用户 JSP 的 pageEncoding 属性一致,以便正确解析表单的内
容,为了让 CommonsMultipartResolver 正确工作,必须先将 Jakarta Commons FileUpload 及 Jakarta Commons io 的类包添加到类路径下。
commons-fileupload-1.2.1.jar commons-io-2.0.jar |
严重: Servlet /SpringMVC_06_FileUpload threw load() exception java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory |
id必须是”multipartResolver”,否则,会报错误: java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured? –> class=”org.springframework.web.multipart.commons.CommonsMultipartResolver“> |
<%@ page language=”java” contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″%>
“http://www.w3.org/TR/html4/loose.dtd”>
文件: 描述: 提交“/>
|
package com.atguigu.springmvc.crud.handlers; import java.io.IOException; import java.io.InputStream; 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.RequestParam; import org.springframework.web.multipart.MultipartFile; @Controller public class UploadHandler { @RequestMapping(value=”/testUpload”,method=RequestMethod.POST) public String testUpload(@RequestParam(value=”desc”,required=false) String desc, @RequestParam(“file”) MultipartFile multipartFile) throws IOException{ System.out.println(“desc : “+desc); System.out.println(“OriginalFilename : “+multipartFile.getOriginalFilename()); InputStream inputStream = multipartFile.getInputStream(); System.out.println(“inputStream.available() : “+inputStream.available()); System.out.println(“inputStream : “+inputStream); return “success”; //增加成功页面: /views/success.jsp } } |
想要了解跟多关于java培训课程内容欢迎关注尚硅谷java培训,尚硅谷除了这些技术文章外还有免费的高质量java培训课程视频供广大学员下载学习。