zoukankan      html  css  js  c++  java
  • springmvc02

    springmvc 文件上传

    1.enctype 取值必须是: multipart/form-data
    2.method为post

    3.提供文件选择域<input type="file">

     1  2
     3 <form action="/user/fileupload2" method="post" enctype="multipart/form-data">
     4     选择文件<input type="file" name="file"/><br/>
     5     <input type="submit" value="上传">
     6 
     7 </form>
     8 
     9     @RequestMapping("/fileupload")              //表单中name属性值和 MultipartFile 的形参必须一致
    10     public String fileupload(HttpServletRequest request, MultipartFile file) throws Exception {
    11         String path = request.getSession().getServletContext().getRealPath("/uploads");
    12         File f = new File(path);
    13         if (!f.exists()){
    14             f.mkdirs();
    15         }
    16 
    17         //upload
    18         String filename = file.getOriginalFilename();
    19         String uuid = UUID.randomUUID().toString().replace("-", "");
    20         filename = uuid + "_"+filename;
    21         file.transferTo(new File(path,filename));
    22 
    23         return "success";
    24 
    25     }

    配置视图解析器

    1     <!--配置文件解析器-->
    2     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    3         <property name="maxUploadSize" value="10485760"/>
    4     </bean>

    跨服务器文件上传,采用本地两个tomcat

     1     @RequestMapping("/fileupload")
     2     public String fileupload3(MultipartFile file) throws Exception {
     3         //定义服务器路径
     4         String path = "http://localhost:9090/uploads/";
     5 
     6         //upload
     7         String filename = file.getOriginalFilename();
     8         String uuid = UUID.randomUUID().toString().replace("-", "");
     9         filename = uuid + "_"+filename;
    10 
    11         //创建客户端对象
    12         Client client = Client.create();
    13         //和服务器连接
    14         WebResource webResource = client.resource(path + filename);
    15         //上传
    16         webResource.put(file.getBytes());
    17         return "success";
    18 
    19     }

    异常处理

    系统的 daoservicecontroller 出现都通过 throws Exception 向上抛出,最后由 springmvc 前端控制器交由异常处理器进行异常处理

    自定义异常

    1 public class SysException extends Exception {
    2 private String messge;
    3 
    4 //getter setter
    5       
    6 //Constructor   (new 异常对象必须传入信息  )
    7 
    8 
    9 }

    error.jsp

     1 <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
     2 <html>
     3 <head>
     4     <title>Title</title>
     5 </head>
     6 <body>
     7 
     8     ${errorMsg}
     9 
    10 </body>
    11 </html>

    自定义异常处理器

     1 public class SysExceptionResolver implements HandlerExceptionResolver {
     2 
     3     @Override
     4     public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
     5         //获取异常对象
     6         SysException e = null;
     7         if (ex instanceof  SysException){
     8             e = (SysException) ex;
     9         }else {
    10             e = new SysException("系统正在维护");
    11         }
    12         //创建ModelandView
    13         ModelAndView mv = new ModelAndView();
    14         mv.addObject("errorMsg",e.getMessage());
    15         mv.setViewName("error");
    16         return mv;
    17 
    18     }
    19 
    20 }

    springmvc.xml 定义异常处理器

    1     <bean id="sysException" class="com.xxx.exception.SysExceptionResolver"/>

    举例

     1     <a href="user/testException">测试异常</a>
     2 
     3     @RequestMapping("/testException")
     4     public String testException() throws Exception{
     5 
     6         try {
     7             int a = 10/0;
     8         } catch (Exception e) {
     9             e.printStackTrace();
    10             //抛出自定义异常信息
    11             throw new SysException("出错了.....");
    12         }
    13         return "success";
    14     }

    拦截器

    用于对处理器进行预处理和后处理

    拦截器&&过滤器

    过滤器servlet 规范中的一部分, 任何 java web 工程都可以使用。
    拦截器SpringMVC 框架自己的,只有使用了 SpringMVC 框架的工程才能用。
    过滤器url-pattern 中配置了/*之后,可以对所有要访问的资源拦截。
    拦截器它是只会拦截访问的控制器方法,如果访问的是 jsphtml,css,image 或者 js 是不会进行拦截的。

    自定义拦截器, 要求必须实现: HandlerInterceptor 接口

     1 public class MyInterceptor implements HandlerInterceptor {
     2 
     3     /**
     4      * 预处理,controller方法拦截前
     5      *
     6      */
     7     @Override
     8     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
     9         System.out.println("prehand执行了...");
    10 
    11         return true;
    12     }
    13 
    14     /**
    15      * 后处理,controller之后
    16      */
    17     @Override
    18     public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
    19         System.out.println("postHandler执行了");
    20     }
    21 }

    springmvc.xml配置拦截器

     1     <!--配置拦截器-->
     2     <mvc:interceptors>
     3         <mvc:interceptor>
     4             <!--拦截的具体方法-->
     5             <mvc:mapping path="/user/*"/>
     6             <!--<mvc:exclude-mapping path=""/>-->
     7             <!--拦截对象-->
     8             <bean class="com.xxx.interceptor.MyInterceptor"></bean>
     9         </mvc:interceptor>
    10     </mvc:interceptors>

    举例

     1 <a href="user/testInterceptor">拦截器</a>
     2 
     3     @RequestMapping("/testInterceptor")
     4     public String testInterceptor(){
     5         System.out.println("testInterceptor执行了...");
     6         return null;
     7     }
     8 
     9 
    10 
    11 
    12 //preHandler执行了
    13 //控制器中方法执行了
    14 //postHandler执行了

     

  • 相关阅读:
    【EF Code First】CodeFirst初始配置
    【HTML5】炫丽的时钟效果Canvas绘图与动画基础练习
    C# MongoDB--时区问题(差了8小时)
    【HTML5】Canvas绘制基础
    判断数组与对象的方法
    javascript中的this
    实现动画效果
    js-改变this的指向
    js预编译
    js对象枚举
  • 原文地址:https://www.cnblogs.com/quyangyang/p/11851500.html
Copyright © 2011-2022 走看看