@Controller
public class MostFileController { //多文件上传
@RequestMapping("/first")
public String doFlrat(@RequestParam MultipartFile[] upload, HttpSession session) {
System.out.println("*******************");
for (MultipartFile item:upload) {
if(item.getSize()>0){
//用户是否选择了文件
//获取到用户上传的文件名称
String chilPath=item.getOriginalFilename(); //文件段名称
if(chilPath.endsWith(".jpg")||chilPath.endsWith("gif")||chilPath.endsWith("png")){
//将行对路径转换成绝对路径
String paraPath=session.getServletContext().getRealPath("/uplode");
//将file写入指定的路径
File filePath=new File(paraPath,chilPath);
try {
//将文件内存运输到指定的文件中
item.transferTo(filePath);
} catch (IOException e) {
e.printStackTrace();
return "/Fileuplode.jsp";
}
}else {
return "/Fileuplode.jsp";
}
}else {
return "/Fileuplode.jsp";
}
}
return "/index.jsp";
}
@RequestMapping("/first2")
public String doFirst2(MultipartFile upload,HttpSession session){
System.out.println("****************************88");
if(upload.getSize()>0){
//用户是否选择了文件
//获取到用户上传的文件名称
String chilPath=upload.getOriginalFilename(); //文件短名称
if(chilPath.endsWith(".jpg")||chilPath.endsWith("gif")||chilPath.endsWith("png")){
//将相对路径转化成绝对路径
String paratPath=session.getServletContext().getRealPath("/uplode");
//将file写入指定的路径
File filePath=new File(paratPath,chilPath);
try {
//将文件内存运输到指定的文件中
upload.transferTo(filePath);
} catch (IOException e) {
e.printStackTrace();
return "/index.jsp";
}
}else {
return "/Fileuplode.jsp";
}
}else {
return "/Fileuplode.jsp";
}
return "/Fileuplode.jsp";
}
}
MostFileupdlo.xml配置:
<!--配置包扫描器-->
<context:component-scan base-package="cn.mostFileupload"></context:component-scan>
<!--配置文件上传的专用类-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="5000000"></property>
</bean>
<mvc:annotation-driven/>
Fileuplode.jsp页面:
</head>
<body>
<h1>文件上传</h1>
<form action="/first" method="post" enctype="multipart/form-data">
文件1 <input type="file" name="upload"/>
文件2 <input type="file" name="upload"/>
文件3 <input type="file" name="upload"/>
<input type="submit"/>
</form>
</body>
Struts2拦截器: exception 异常拦截器
params 参数拦截器
il8n 国际化拦截器
fileupload 文件上传拦截器
validation 校验拦截器
Struts2中处理的请求的组件是:Action
SpringMVC中处理请求的组件是:Controller
JSP中处理请求的组件是: servlet
拦截器HandlerInterceptor的三种方法:(1)perHandle() (2)postHandle() (3)afterCompletion()
注册拦截器: **匹配0或者更多的目录
*匹配0或者任意的字符串
拦截器:
创建Myhanderinter类并集成HandlerInterceptor接口中的方法:
public class Myhanderinter implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("perHandle+=========================================");
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("posthandle-------------------------------------");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("afterHandle====================================");
}
}
//在创建一个intercfeption类
@Controller
public class intercfeption {
@RequestMapping("/first")
public String doInter(){
System.out.println("Handle=====================================");
return "index.jsp";
}
}
HandleInter.xml配置:
<!--配置包扫描器-->
<context:component-scan base-package="cn.Handerinter"></context:component-scan>
<!--注册拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="cn.Handerinter.Myhanderinter"></bean>
</mvc:interceptor>
</mvc:interceptors>
<mvc:annotation-driven/>