SpringMVC拦截器
1. 创建拦截类,实现HandlerInterceptor接口
package com.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/*
* 创建拦截器类,实现HandlerInterceptor
*/
public class TestInterceptor implements HandlerInterceptor{
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("处理请求之后.....");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
System.out.println("处理请求中.....");
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
System.out.println("处理请求之前.....");
// true 是指将请求进行释放传递
return true;
}
}
2. 在配置文件中配置拦截器
<!-- 配置拦截器 -->
<mvc:interceptors>
<!-- 注入拦截器类,拦截所有请求 -->
<bean class="com.interceptor.TestInterceptor"></bean>
<mvc:interceptor>
<!-- 拦截指定路径下的请求 -->
<mvc:mapping path="/test/one.do"/>
<bean class="com.interceptor.TestInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
SpringMVC上传
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传</title>
</head>
<body>
<form action="test/upload.do" enctype="multipart/form-data" method="post">
说明: <input type="text" name="show" /><br /><br />
文件: <input type="file" name="file" value="" /><br /><br />
<input type="submit" />
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<context:component-scan base-package="com.controller"></context:component-scan>
<bean id="urlBasedViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- SpringMVC文件上传配置:id为固定值 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="1024000"></property>
</bean>
</beans>
package com.controller;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping("/test")
public class Test {
@RequestMapping("/upload.do")
public String upload(String show ,@RequestParam("file")MultipartFile file,HttpServletRequest request){
// 获取项目的绝对路径拼接文件名字
String filePath = request.getSession().getServletContext().getRealPath(File.separator)+file.getOriginalFilename();
// 将文件写到指定路径下的文件中
try {
file.transferTo(new File(filePath));
} catch (IllegalStateException e) {
System.err.println("文件参数错误");
} catch (IOException e) {
System.err.println("文件操作流失败");
}
// 获取会话,写入文件名字
HttpSession session = request.getSession();
session.setAttribute("file",file.getOriginalFilename());
return "ok";
}
}
<%@ 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>
<title>加载图片</title>
</head>
<body>
<h1>文件上传成功</h1>
<!-- 根据URL路径访问图片 -->
<img alt="文件加载失败" src="<%=basePath %>${file}" />
</body>
</html>