文件上传
三种上传方案
虚拟路径与真实路径 /upload
copyFile与copydirectory
commons-io
commons-io
2.4
commons-fileupload
commons-fileupload
1.3.1
将工程所需要的jar包进行引入
Spring上下文文件的配置
Jsp页面的测试
通常采用form表单通过post的方式进行提交但是需要配置一个enctype的属性,属性值为“multipart/form-data”这样可以将file文件进行提交
用户姓名:
文件:
Controller层对功能的实现
@RequestMapping("/iotest")
public StringioTest(@RequestParam(value=“userPhoto”,required=false) MultipartFile userPhoto){
System.out.println("------------>"+userPhoto);
return “/io/success”;
}
结果:
------------>org.springframework.web.multipart.commons.CommonsMultipartFile@383ea189
------------>org.springframework.web.multipart.commons.CommonsMultipartFile@d1f978f
package mvc.data.io.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class InterceptorTest implements HandlerInterceptor{
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("-----preHandle------");
return false;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("-----postHandle------");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("-----afterCompletion------");
}
}
在Spring上下文中的配置,需要将拦截器配置到上下文中
mvc:interceptors
mvc:interceptor
在public boolean preHandle这个方法中如果返回值是false则只会运行到该方法结束程序运行就会结束。