标签:种类 throws tip exists return void width div tac
1.文件上传
1.1导入jar包

1.2设置表单提交属性
文件上传只允许表单为post提交,并且编码类型为multipart/form-data

1.3在springmvc中配置文件上传解析器。
其中的id名不能更改,否则报错
设置最大上传大小maxUploadSize

1.4 在控制层处理代码
@RequestMapping("upload")
public String upload(MultipartFile myfile,HttpServletRequest request) {
//1.获取文件上传真实保存路径
String path=request.getServletContext().getRealPath("/upload");
//2.创建一个文件对象
File file=new File(path);
if(!file.exists()) {
file.mkdirs();
}
//3.获取文件名
String name=System.currentTimeMillis()+myfile.getOriginalFilename();
File targetFile=new File(path+"/"+name);
//4.把文件写入到指定的目录
try {
FileUtils.writeByteArrayToFile(targetFile, myfile.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "index";
}
2.拦截器的使用(拦截都是控制层的地址。 filter: )
2.1 创建一个类 实现接口 HandlerInterceptor,重写里面的方法
public class MyInterceptor implements HandlerInterceptor{ @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { } @Override public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { return false; }
2.2 把创建的类配置到springmvc文件中

3.数据校验(后台校验)
3.1引入jar包

3.2在相应的实体类中加入注解

注解的种类:

3.3在控制层接受参数时

SpringMVC-------3.文件上传,拦截器和数据校验(后台校验)
标签:种类 throws tip exists return void width div tac
原文地址:https://www.cnblogs.com/zyl187110/p/11462175.html