<form action="${pageContext.request.contextPath}/test/upload.do" method="post" enctype="multipart/form-data">
<input type="file" name="img"><br />
<input type="submit" name="提交">
</form>
@Controller
@RequestMapping("/test")
public class MyController {
@RequestMapping(value = "/upload.do", method = RequestMethod.POST)
// 这里的MultipartFile对象变量名跟表单中的file类型的input标签的name相同,所以框架会自动用MultipartFile对象来接收上传过来的文件,当然也可以使用@RequestParam("img")指定其对应的参数名称
public String upload(MultipartFile img, HttpSession session)
throws Exception {
// 如果没有文件上传,MultipartFile也不会为null,可以通过调用getSize()方法获取文件的大小来判断是否有上传文件
if (img.getSize() > 0) {
// 得到项目在服务器的真实根路径,如:/home/tomcat/webapp/项目名/images
String path = session.getServletContext().getRealPath("images");
// 得到文件的原始名称,如:美女.png
String fileName = img.getOriginalFilename();
// 通过文件的原始名称,可以对上传文件类型做限制,如:只能上传jpg和png的图片文件
if (fileName.endsWith("jpg") || fileName.endsWith("png")) {
File file = new File(path, fileName);
img.transferTo(file);
return "/success.jsp";
}
}
return "/error.jsp";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
...
<!-- 注意:CommonsMultipartResolver的id是固定不变的,一定是multipartResolver,不可修改 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 如果上传后出现文件名中文乱码可以使用该属性解决 -->
<property name="defaultEncoding" value="utf-8"/>
<!-- 单位是字节,不设置默认不限制总的上传文件大小,这里设置总的上传文件大小不超过1M(1*1024*1024) -->
<property name="maxUploadSize" value="1048576"/>
<!-- 跟maxUploadSize差不多,不过maxUploadSizePerFile是限制每个上传文件的大小,而maxUploadSize是限制总的上传文件大小 -->
<property name="maxUploadSizePerFile" value="1048576"/>
</bean>
<!-- 设置一个简单的异常解析器,当文件上传超过大小限制时跳转 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="/error.jsp"/>
</bean>
</beans>
<form action="${pageContext.request.contextPath}/test/upload.do" method="post" enctype="multipart/form-data">
file 1 : <input type="file" name="imgs"><br />
file 2 : <input type="file" name="imgs"><br />
file 3 : <input type="file" name="imgs"><br />
<input type="submit" name="提交">
</form>
@Controller
@RequestMapping("/test")
public class MyController {
@RequestMapping(value = "/upload.do", method = RequestMethod.POST)
// 这里的MultipartFile[] imgs表示前端页面上传过来的多个文件,imgs对应页面中多个file类型的input标签的name,但框架只会将一个文件封装进一个MultipartFile对象,
// 并不会将多个文件封装进一个MultipartFile[]数组,直接使用会报[Lorg.springframework.web.multipart.MultipartFile;.<init>()错误,
// 所以需要用@RequestParam校正参数(参数名与MultipartFile对象名一致),当然也可以这么写:@RequestParam("imgs") MultipartFile[] files。
public String upload(@RequestParam MultipartFile[] imgs, HttpSession session)
throws Exception {
for (MultipartFile img : imgs) {
if (img.getSize() > 0) {
String path = session.getServletContext().getRealPath("images");
String fileName = img.getOriginalFilename();
if (fileName.endsWith("jpg") || fileName.endsWith("png")) {
File file = new File(path, fileName);
img.transferTo(file);
}
}
}
return "/success.jsp";
}
}
<form action="${pageContext.request.contextPath}/test/upload.do" method="post" enctype="multipart/form-data">
一次选择多个文件的多文件上传 : <br />
<input type="file" name="imgs1" multiple><br /> <br />
一次选择一个文件的多文件上传 : <br />
<input type="file" name="imgs2"><br />
<input type="file" name="imgs2"><br /><br />
单文件上传 : <br />
<input type="file" name="imgs3"><br /><br />
<input type="submit" name="提交">
</form>
@Controller
@RequestMapping("/test")
public class MyController {
@RequestMapping(value = "/upload.do", method = RequestMethod.POST)
public String upload(@RequestParam MultipartFile[] imgs1,@RequestParam MultipartFile[] imgs2,@RequestParam MultipartFile[] imgs3, HttpSession session)
throws Exception {
String path = session.getServletContext().getRealPath("images");
for (MultipartFile img : imgs1) {
uploadFile(path, img);
}
for (MultipartFile img : imgs2) {
uploadFile(path, img);
}
for (MultipartFile img : imgs3) {
uploadFile(path, img);
}
return "/success.jsp";
}
private void uploadFile(String path, MultipartFile img) throws IOException {
if (img.getSize() > 0) {
String fileName = img.getOriginalFilename();
if (fileName.endsWith("jpg") || fileName.endsWith("png")) {
File file = new File(path, fileName);
img.transferTo(file);
}
}
}
}
String getContentType()//获取文件MIME类型 InputStream getInputStream()//获取文件流 String getName() //获取表单中文件组件的名字 String getOriginalFilename() //获取上传文件的原名 long getSize() //获取文件的字节大小,单位byte boolean isEmpty() //是否为空 void transferTo(File dest)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有