Spring MVC 学习笔记 八 文件上传

Spring mvc使用jakarta的commons fileupload来支持文件上传。
先修改pom.xml,增加对commons fileupload lib包的引用。

		<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>

再修改spring的context 配置文件,增加对MultipartResolver的引用。
	<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000" />
</bean>


增加uploader的controller类
	@RequestMapping("/uploader")
public String uploader(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file,
HttpSession session){
if (!file.isEmpty()) {
System.out.println(file.getSize());
}
return "helloWorld";
}

增加文件上传的jsp页面
<html>
<head>
<title>file upload test</title>
</head>
<body>

<form method="post" action="uploader" enctype="multipart/form-data">
<input type="text" name="name" />
<input type="file" name="file" />
<input type="submit" />
</form>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值