文件上传下载
导入maven
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
配置MultipartResolver
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为5MB -->
<property name="maxUploadSize" >
<value>5242880</value>
</property>
</bean>
创建表单
<form action="uploadFile" method="post" enctype="multipart/form-data">
文件:<input type="file" name="file"><br>
<input type="submit" value="上传">
</form>
创建Controller
@RequestMapping("uploadFile")
public String uploadFile(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request){
//文件上传的位置 服务器路径
String path = request.getSession().getServletContext().getRealPath("resources/upload");
//System.out.println(path);
if(!file.isEmpty()){
try {
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path, file.getOriginalFilename()));
} catch (IOException e) {
e.printStackTrace();
}
}
return "index";
}
配置文件问题
静态资源问题
方式一:修改前端控制器 不推荐 web.xml
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
方式二:追加默认放行的资源 不推荐 (有多少写多少)
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
方式三:修改SpringMVC-servlet.xml文件
追加mvc的命名空间
xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
//mvc驱动
//mapping:映射路径
//location:真实路径
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.hwq"></context:component-scan>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<!-- 视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
异常处理
局部异常
作用于当前控制器
@ExceptionHandler({Exception.class})
public String excpetion(Exception a){
System.out.println("抛出异常");
a.printStackTrace();
return "redirect:error.jsp";
//return "error";
}
全局异常
作用于整个项目
<!-- 异常解析器 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">redirect:error.jsp</prop>
</props>
</property>
</bean>
拦截器
第一步:创建类,继承拦截器适配器类或实现拦截器接口
第二步:重写方法
public class MyInterceptor implements HandlerInterceptor {
/*
登录拦截
false表示拦截,不向下执行
true表示放行
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截器执行了");
//判断是否合法 登录路径 session有数据
//request.getRequestURI().equals("/login");
//request.getSession().getAttribute("user");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("拦截器执行结束了");
}
}
第三步:配置拦截器
<!-- 拦截器配置-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<bean id="myInterceptor" class="com.hwq.interceptor.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>