Spring MVC(四)文件上传
文件上传步骤
1.写一个文件上传的页面
2.写一个文件上传的控制器
注意:
1.method="post"
2.enctype="multipart/form-data"
3.文件类型上传组件 type="file"
4.接收文件参数需要使用MultipartFile 类型的参数
5.配置文件解析器,文件解析器 id 必须是 "multipartResolver"
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="upload.do" method="post" enctype="multipart/form-data"> 11 姓名:<input type="text" name="username"><br> 12 头像:<input type="file" name="mf"><br> 13 <input type="submit" value="上传"> 14 </form> 15 </body> 16 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h1>欢迎进入</h1> 11 <img alt="程序猿" src="${imgPath }"> 12 </body> 13 </html>