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>
分享到:
相关推荐
spring mvc学习笔记spring mvc学习笔记spring mvc学习笔记spring mvc学习笔记spring mvc学习笔记
传智播客 Spring MVC 学习笔记
spring mvc的学习笔记,主要讲解了基于xml的配置,和如何增加spring mvc拦截器,如何返回json报文
Spring3 MVC 学习笔记 入门 Spring3 MVC 学习笔记 入门
spring mvc学习笔记,包括学习遇到的问题和解决方法。
这是个人的Spring MVC的MD版学习笔记+源码文件,具体参考:https://blog.csdn.net/m0_46153949/article/details/114260215
工程文件 博文链接:https://starscream.iteye.com/blog/1067606
NULL 博文链接:https://starscream.iteye.com/blog/1072180
sample code 博文链接:https://starscream.iteye.com/blog/1057305
工程文件 博文链接:https://starscream.iteye.com/blog/1072179
NULL 博文链接:https://mengshadow.iteye.com/blog/1826299
工程文件 博文链接:https://starscream.iteye.com/blog/1063966
自己学习spring课程的笔记。笔记都是根据尚硅谷的课程(spring ioc,spring aop,spring mvc,spring boot等)写的。 主要内容:spring ioc,spring aop,spring mvc,spring boot
Spring MVC 3学习笔记+教程 这是一个学习spring mvc设计模式很好的参考文档,希望大家可以从中学到东西
基于注解的spring_3.0.x_MVC学习笔记