【SpringMVC筆記】攔截器 + 文件上傳下載

更多內容參見 SpringMVC 學習筆記目錄:【Spring MVC】學習筆記彙總

攔截器(interceptor)

SpringMVC 的處理器攔截器類似於 Servlet 開發中的過濾器 Filter,用於對處理器進行 預處理 和 後處理。開發者可以自己定義一些攔截器來實現特定的功能。

過濾器與攔截器的區別:攔截器是 AOP 思想的具體應用。

過濾器(filter):

  • servlet 規範中的一部分,任何 java web 工程都可以使用;
  • <url-pattern> 中配置了 /* 之後,可以對所有要訪問的資源進行攔截;

攔截器(interceptor):

  • 攔截器屬於 SpringMVC 框架的,只有使用了 SpringMVC 框架的工程才能使用;
  • 攔截器只會攔截訪問的控制器方法, 如果訪問的是 jsp/html/css/image/js 是不會進行攔截的;

自定義攔截器

想要自定義攔截器,必須實現 HandlerInterceptor 接口。

  1. 配置 web.xml 和 springmvc-servlet.xml 文件
  2. 編寫一個攔截器:
package com.yusael.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor {
    // 在請求處理的方法之前執行
    // 如果返回 true 執行下一個攔截器(放行)
    // 如果返回 false 就不執行下一個攔截器
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("---------處理前---------");
        return true;
    }

    // 在請求處理方法執行之後執行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("---------處理後---------");
    }

    // 在 dispatcherServlet 處理後執行, 做清理工作
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("---------清理---------");
    }

}
  1. 在 SpringMVC 的配置文件中配置攔截器:
<!--關於攔截器的配置-->
<mvc:interceptors>
    <mvc:interceptor>
        <!--/** 包括路徑及其子路徑-->
        <!--/admin/* 攔截的是/admin/add等等這種 , /admin/add/user不會被攔截-->
        <!--/admin/** 攔截的是/admin/下的所有-->
        <mvc:mapping path="/**"/>
        <!--bean配置的就是攔截器-->
        <bean class="com.yusael.interceptor.MyInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>
  1. 編寫一個 Controller,接收請求:
package com.yusael.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

// 測試攔截器的控制器
@RestController
public class InterceptorController {

    @RequestMapping("/interceptor")
    public String testFunction() {
        System.out.println("控制器中的方法執行了!");
        return "hello";
    }

}
  1. 前端 index.jsp
<a href="${pageContext.request.contextPath}/interceptor">攔截器測試</a>
  1. 啓動 Tomcat 進行測試。

如果自定義攔截器中 preHandle 返回 false,表示不放行,被攔截了,不繼續執行了。在這裏插入圖片描述
如果自定義攔截器中 preHandle 返回 true,表示放行,繼續執行後面的代碼。
在這裏插入圖片描述

驗證用戶是否登錄 (認證用戶)

思路:

  1. 有一個登陸頁面,需要寫一個 Controller 訪問頁面。
  2. 登陸頁面有一個提交表單的動作,需要在 Controller 中處理;
    判斷用戶名密碼是否正確:如果正確,向 session 中寫入用戶信息,返回登陸成功。
  3. 攔截用戶請求,判斷用戶是否登陸。
    如果用戶已經登陸,放行;
    如果用戶未登陸,跳轉到登陸頁面;

沒有攔截器

如果沒有攔截器,也就是上面思路中的 1 和 2 步,用戶未登錄也可以進入主頁

  1. 編寫一個登陸頁面 login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>login</title>
</head>

<h1>登錄頁面</h1>
<hr>

<body>
<form action="${pageContext.request.contextPath}/user/login">
    用戶名:<input type="text" name="username"> <br>
    密碼:<input type="password" name="pwd"> <br>
    <input type="submit" value="提交">
</form>
</body>
</html>
  1. 編寫一個 Controller 處理請求
package com.yusael.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    // 跳轉到登錄界面
    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    // 跳轉到成功頁面
    @RequestMapping("/toSuccess")
    public String toSucess() {
        return "success";
    }

    // 登錄提交
    @RequestMapping("/login")
    public String login(HttpSession session, String username, String password) {
        // 向 session 記錄用戶身份信息
        System.out.println("接收前端 ===> " + username);
        session.setAttribute("user", username);
        return "success";
    }

    // 登錄過期
    @RequestMapping("/logout")
    public String logout(HttpSession httpSession) {
        // session 過期
        httpSession.invalidate();
        return "login";
    }

}
  1. 編寫一個登陸成功的頁面 success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>success</title>
</head>
<body>

<h1>登錄成功頁面</h1>
<hr>

${user}
<a href="${pageContext.request.contextPath}/user/logout">註銷</a>
</body>
</html>
  1. 編寫主頁 index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>index</title>
</head>
<body>
<h1>首頁</h1>
<hr>
<%--登錄--%>
<a href="${pageContext.request.contextPath}/user/toLogin">登錄</a>
<a href="${pageContext.request.contextPath}/user/toSuccess">成功頁面</a>
</body>
</html>

測試:
在這裏插入圖片描述
點擊 登錄,會請求跳轉到 登陸頁面 login.jsp;
點擊 成功頁面,會請求跳轉到 成功頁面 success.jsp

這裏就出現問題了,只要在 地址欄發出請求,可以直接 越過登錄,進入登錄成功的界面,這個肯定是有問題的。那要如何解決這個問題呢? —— 通過 攔截器

配置攔截器

  1. 編寫用戶登錄攔截器 LoginInterceptor.java
package com.yusael.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 如果是登錄頁面則放行
        System.out.println(request.getRequestURI());
        if (request.getRequestURI().contains("login")) {
            return true;
        }

        HttpSession session = request.getSession();
        // 如果用戶已經登錄也放行
        if (session.getAttribute("user") != null) {
            return true;
        }

        // 如果用戶沒有登錄則跳轉到登錄界面
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
  1. 在 Springmvc 的配置文件中註冊攔截器:
<!--關於攔截器的配置-->
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean id="loginInterceptor" class="com.yusael.interceptor.LoginInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

測試:
在這裏插入圖片描述
由於配置了 攔截器,直接登錄 成功頁面,雖然會請求跳轉到 success.jsp 頁面,但是由於不滿足攔截器進行了判斷,不滿足放行的條件則會跳轉到 登錄界面。只有滿足攔截器放行的條件纔會跳轉到 成功頁面。

文件的上傳

文件上傳是項目開發中最常見的功能之一 ,SpringMVC 可以很好的支持文件上傳,但是 SpringMVC 上下文中默認沒有裝配 MultipartResolver,因此默認情況下其不能處理文件上傳工作。如果想使用Spring 的文件上傳功能,則需要在上下文中配置 MultipartResolver

對前端表單的要求:爲了能上傳文件,必須將表單的 method 設置爲 POST,並將 enctype 設置爲multipart/form-data。只有在這樣的情況下,瀏覽器纔會把用戶選擇的文件以二進制數據發送給服務器;

表單中的 enctype (編碼方式)屬性的說明:

  • application/x-www=form-urlencoded:默認方式,只處理表單域中的 value 屬性值,採用這種編碼方式的表單會將表單域中的值處理成 URL 編碼方式。
  • multipart/form-data:這種編碼方式會以二進制流的方式來處理表單數據,這種編碼方式會把文件域指定文件的內容也封裝到請求參數中,不會對字符編碼。
  • text/plain:除了把空格轉換爲 “+” 號外,其他字符都不做編碼處理,這種方式適用直接通過表單發送郵件。
<form action="" enctype="multipart/form-data" method="post">
   <input type="file" name="file"/>
   <input type="submit">
</form>

一旦設置了 enctypemultipart/form-data,瀏覽器即會採用二進制流的方式來處理表單數據,而對於文件上傳的處理則涉及在服務器端解析原始的 HTTP 響應。

在2003年,Apache Software Foundation 發佈了開源的 Commons FileUpload 組件,其很快成爲Servlet/JSP 程序員上傳文件的最佳選擇。

Servlet3.0 規範已經提供方法來處理文件上傳,但這種上傳需要在 Servlet 中完成;而Spring MVC則提供了更簡單的封裝;

Spring MVC 爲文件上傳提供了直接的支持,這種支持是用即插即用的 MultipartResolver 實現的。

Spring MVC 使用 Apache Commons FileUpload 技術實現了一個 MultipartResolver 實現類:
CommonsMultipartResolver。因此,SpringMVC 的文件上傳還需要依賴 Apache Commons FileUpload 的組件。

文件輸出流進行文件上傳

  1. 引入 commons-fileupload 的依賴
<!--文件上傳-->
<dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
   <version>1.3.3</version>
</dependency>
<!--servlet-api導入高版本的-->
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>4.0.1</version>
</dependency>
  1. 配置 bean:multipartResolver
    注:這個 bean 的 id 必須爲:multipartResolver,否則上傳文件會報 400 的錯誤。
<!--文件上傳配置-->
<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   <!-- 請求的編碼格式, 必須和 jSP 的 pageEncoding 屬性一致, 以便正確讀取表單的內容, 默認爲ISO-8859-1 -->
   <property name="defaultEncoding" value="utf-8"/>
   <!-- 上傳文件大小上限,單位爲字節(10485760=10M) -->
   <property name="maxUploadSize" value="10485760"/>
   <property name="maxInMemorySize" value="40960"/>
</bean>

CommonsMultipartFile 的 常用方法:

String getOriginalFilename() 獲取上傳文件的原名
InputStream getInputStream() 獲取文件流
void transferTo(File dest)   將上傳文件保存到一個目錄文件中
  1. 編寫前端頁面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>file</title>
  </head>
  <body>
    <form action="/upload" enctype="multipart/form-data" method="post">
      <input type="file" name="file"/>
      <input type="submit" value="upload">
    </form>
  </body>
</html>
  1. Controller
package com.yusael.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

@Controller
public class FileController {
    // @RequestParam("file") 將 name=file 控件得到的文件封裝成 CommonsMultipartFile 對象
    // 批量上傳 CommonsMultipartFile 則爲數組即可
    @RequestMapping("/upload")
    public String fileUpload(@RequestParam("file")CommonsMultipartFile file, HttpServletRequest request) throws IOException {
        // 獲取文件名
        String uploadFileName = file.getOriginalFilename();

        // 如果文件名爲空, 直接回到首頁
        if ("".equals(uploadFileName)){
            return "redirect:/index.jsp";
        }

        System.out.println("上傳文件名: " + uploadFileName);

        // 上傳路徑保存設置
        String path = request.getServletContext().getRealPath("/upload");
        // 如果路徑不存在, 則創建一個
        File realPath = new File(path);
        if (!realPath.exists()) {
            realPath.mkdir();
        }
        System.out.println("上傳文件保存地址: " + realPath);

        // 文件輸入流
        InputStream is = file.getInputStream();
        // 文件輸出流
        OutputStream os = new FileOutputStream(new File(realPath, uploadFileName));

        // 讀取寫出
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
            os.flush();
        }
        os.close();
        is.close();
        return "redirect:/index.jsp";
    }

}

測試文件上傳:
在這裏插入圖片描述
在 out 目錄下可以找到我們設定的下載地址:
在這裏插入圖片描述

file.Transto 進行文件上傳

Controller:

@RequestMapping("/upload2")
public String fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
    // 上傳路徑保存設置
    String path = request.getServletContext().getRealPath("/upload");
    File realPath = new File(path);
    if (!realPath.exists()) {
        realPath.mkdir();
    }
    System.out.println("上傳文件保存地址: " + realPath);

    // 通過 CommonsMultipartFile 的方法直接寫文件
    file.transferTo(new File(realPath + "/" + file.getOriginalFilename()));

    return "redirect:/index.jsp";
}

前端頁面 index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>file</title>
  </head>
  <body>
    <form action="/upload" enctype="multipart/form-data" method="post">
      <input type="file" name="file"/>
      <input type="submit" value="upload">
    </form>
    <hr>
    <form action="/upload2" enctype="multipart/form-data" method="post">
      <input type="file" name="file"/>
      <input type="submit" value="upload">
    </form>
  </body>
</html>

進行測試:
在這裏插入圖片描述
效果與上面的一模一樣,同樣可以在 out 目錄的對應位置找到上傳的文件。
在這裏插入圖片描述

文件的下載

文件下載步驟:

  1. 設置 response 響應頭
  2. 讀取文件 – InputStream
  3. 寫出文件 – OutputStream
  4. 執行操作
  5. 關閉流 (先開後關)

代碼實現:

@RequestMapping("/download")
public String downloads(HttpServletResponse response , HttpServletRequest request) throws Exception{
    // 要下載的圖片地址
    String  path = request.getServletContext().getRealPath("/upload");
    String  fileName = "SpringBoot筆記.pdf";

    // 1、設置response 響應頭
    response.reset(); // 設置頁面不緩存, 清空buffer
    response.setCharacterEncoding("UTF-8"); // 字符編碼
    response.setContentType("multipart/form-data"); // 二進制傳輸數據
    // 設置響應頭
    response.setHeader("Content-Disposition",
            "attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8"));

    File file = new File(path,fileName);

    // 2、讀取文件--輸入流
    InputStream input = new FileInputStream(file);

    // 3、寫出文件--輸出流
    OutputStream out = response.getOutputStream();

    byte[] buff = new byte[1024];
    int len = 0;

    // 4、執行 寫出操作
    while ((len = input.read(buff)) != -1) {
        out.write(buff, 0, len);
        out.flush();
    }
    out.close();
    input.close();
    return null;
}

前端頁面 index.jsp:

<a href="/download">點擊下載</a>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章