1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 <!DOCTYPE HTML> 7 <html> 8 <head> 9 <base href="<%=basePath%>"> 10 11 <title>My JSP 'index.jsp' starting page</title> 12 <meta http-equiv="pragma" content="no-cache"> 13 <meta http-equiv="cache-control" content="no-cache"> 14 <meta http-equiv="expires" content="0"> 15 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 16 <meta http-equiv="description" content="This is my page"> 17 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 22 </head> 23 <body> 24 <form action="user/login" method="post" enctype="multipart/form-data"> 25 <input type="file" name="imgs"/> 26 <input type="file" name="imgs"/> 27 <input type="file" name="imgs"/> 28 <button>上传文件</button> 29 </form> 30 31 </body> 32 </html>
index.jsp文件上传的页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="user/login" method="post" enctype="multipart/form-data"> <input type="file" name="imgs"/> <input type="file" name="imgs"/> <input type="file" name="imgs"/> <button>上传文件</button> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1>webroot success页面</h1> </body> </html>
success.jsp成功界面
@Controller @RequestMapping("/user") public class MyController { /** * 单个文件上传 * MultipartFile img :必须和表单中的name属性值一致 * @throws Exception * @throws IllegalStateException @RequestMapping(value = "/login") public String login(MultipartFile img, HttpSession session) throws IllegalStateException, Exception { System.out.println("进入了login......"); // 获取上传的位置 String path = session.getServletContext().getRealPath("/images"); // 获取文件的名称 String filename = img.getOriginalFilename(); System.out.println(img.getOriginalFilename()); System.out.println(img.getName()); // 表单中name的属性值 System.out.println(img.getContentType()); // 上传文件的类型 // 上传文件只能是图片 通过MIME类型 来判断 if (img.getContentType().equals("image/jpeg")) { img.transferTo(new File(path, filename)); // 上传 } return "/success.jsp"; }*/ /** * 多个文件上传 * @RequestParam 必须使用! [email protected] * 默认前台的每一个imgs都是一个数组! * 我们想要的是前台的所有上传文件 在一个数组里面! */ @RequestMapping(value = "/login") public String login(@RequestParam MultipartFile[] imgs, HttpSession session) throws IllegalStateException, Exception { System.out.println("进入了login......"); // 获取上传的位置 String path = session.getServletContext().getRealPath("/images"); // 循环获取文件的名称 for (MultipartFile img : imgs) { // 判断用户是否选择文件 if (img.getSize() > 0) { String filename = img.getOriginalFilename(); img.transferTo(new File(path, filename)); // 上传 } } return "/success.jsp"; } }
对应的controller代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 配置需要扫描的包 --> <context:component-scan base-package="cn.bdqn"/> <!-- 开启注解 --> <mvc:annotation-driven/> <!-- 配置文件上传 id必须是multipartResolver 因为已经在中央调度器 中定义了 Well-known name for the MultipartResolver object in the bean factory for this namespace. */ public static final String MULTIPART_RESOLVER_BEAN_NAME = "multipartResolver"; --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"/><!--乱码问题 --> <property name="maxUploadSize" value="1048576"/><!-- 文件总大小不能超过 1M--> <!-- <property name="maxUploadSizePerFile" value="1048576"/>单个文件不能超过1m --> </bean> </beans>
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/12159.html