Jsp页面编辑
<h1>文件上传入门案例</h1>
<!-- 开启多媒体标签 -->
<form action="http://localhost:8091/file" method="post"
enctype="multipart/form-data">
<input name="image" type="file"/>
<button type="submit" >提交</button>
</form>
文件上传视图解析器
<!-- 文件上传视图解析器
要求bean的id必须为multipartResolver
规范参数:
1.定义最大的上传的数量
2.定义字符集编码
-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
controller层
@RequestMapping("/file")
public String imageFile(MultipartFile image) throws IOException{
//1.定义文件长传的目录 D:jt-upload
File imageFile= new File("D:/jt-upload");
//2.判断文件夹是否存在
if(!imageFile.exists()){
//穿件文件夹
imageFile.mkdirs();
}
//3.获取文件名称
String fileName = image.getOriginalFilename();
//4.实现文件长传 文件路径 、文件名
image.transferTo(new File("D:/jt-upload/"+fileName));
System.out.println("文件长传成功");
//跳转
return"index";
/*//3.获取文件流信息
//InputStream inputStream = image.getInputStream();
*/
}