SpringMVC file upload limit is not logged in

SpringMVC in file upload

1) and the introduction of common-fileupload-IO Commons
2) adding MVC profile:

<!--配置上传的解析器-->
<bean id="multipartResolver"
 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   <property name="maxUploadSize" value="10485760"/>
   <property name="defaultEncoding" value="UTF-8"/>
</bean>

3) in the form tag is added: the enctype = "multipart / form-Data"
. 4) The name provided a form element and entity class attributes, in addition to file file field, file domain name if and entity class attributes, as has occurred 400 error
5) the preparation of upload method

@RequestMapping("addUser")
public String addUser(User user,@RequestParam(value = "imgs",required = false) CommonsMultipartFile imgs){
    if(imgs != null){
        //获得原始文件名
        String name = imgs.getOriginalFilename();
        String suffix = name.substring(name.lastIndexOf("."));
        //使用时间替换原来文件名,避免重复
        String filename = System.currentTimeMillis() + suffix;
        //创建本地文件对象
        File destFile = new File("C:\\xpp\\apache-tomcat-8.0.52\\webapps\\imgs\\" + filename);
        //把上传文件保存到目标文件中
        try {
            imgs.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        user.setImg(filename);
    }
    ser.insert(user);
    return "redirect:page?pageNo=1";
}

Intercept request is not logged in

1, save after a successful login to the user object in Session
2, intercept all requests to determine whether there Session of users, users on the release, no user is forced to jump to the login page
Note: You can not intercept and login and registration related request, external static resources

Defines interception:

/**
 * 用户登录拦截器
 * 拦截没有登录的URL请求
 */
public class UserLoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("拦截了:" + httpServletRequest.getRequestURL());
        //从Session获得用户,判断用户为空,就强制登录,否则放行
        User user = (User) httpServletRequest.getSession().getAttribute("user");
        if(user == null){
            System.out.println("未登录的请求:" + httpServletRequest.getRequestURL());
            httpServletResponse.sendRedirect("http://localhost:8080/ssm/user/index");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}

Configuring interceptors:

<mvc:interceptors>
   <!--配置验证用户登录的拦截器-->
   <mvc:interceptor>
      <!--拦截所有URL-->
      <mvc:mapping path="/**"/>
      <!--不拦截和登录注册相关的URL-->
      <mvc:exclude-mapping path="/**/code.do"/>
      <mvc:exclude-mapping path="/**/*.css"/>
      <mvc:exclude-mapping path="/**/*.js"/>
      <mvc:exclude-mapping path="/**/index"/>
      <mvc:exclude-mapping path="/**/login"/>
      <bean class="com.qianfeng.ssm.interceptor.UserLoginInterceptor"/>
   </mvc:interceptor>
</mvc:interceptors>

Guess you like

Origin www.cnblogs.com/macht/p/11681237.html