• SpringMvc高级(拦截器和文件上传下载)


    拦截器(Interceptor)是一种常用的设计模式,在软件工程领域被广泛应用。拦截器通常被用来处理各种请求和响应,可以在请求发送前或响应返回后进行一系列的操作和处理。拦截器可以用于请求身份认证、日志记录、性能优化、权限控制、数据加密等等,是很多框架和中间件中常用的一种组件。

    在Java中,拦截器一般是通过实现javax.servlet.Filter接口或Spring框架中的HandlerInterceptor接口来实现的。拦截器可以被用于Web应用程序、消息队列、RPC调用等各种情况。

    1. import javax.servlet.*;
    2. import javax.servlet.http.HttpServletRequest;
    3. import javax.servlet.http.HttpServletResponse;
    4. import java.io.IOException;
    5. public class MyInterceptor implements Filter {
    6. @Override
    7. public void init(FilterConfig filterConfig) throws ServletException {
    8. // 初始化操作,可以为空
    9. }
    10. @Override
    11. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    12. HttpServletRequest httpRequest = (HttpServletRequest) request;
    13. HttpServletResponse httpResponse = (HttpServletResponse) response;
    14. // 在此处可以进行各种拦截操作,例如身份认证、权限控制、日志记录等等
    15. // 这里只是一个简单的示例:在请求头中添加一个自定义的字段
    16. httpResponse.setHeader("My-Interceptor", "Hello World!");
    17. chain.doFilter(request, response);
    18. }
    19. @Override
    20. public void destroy() {
    21. // 销毁操作,可以为空
    22. }
    23. }

    1. <filter>
    2. <filter-name>MyInterceptorfilter-name>
    3. <filter-class>com.example.MyInterceptorfilter-class>
    4. filter>
    5. <filter-mapping>
    6. <filter-name>MyInterceptorfilter-name>
    7. <url-pattern>*url-pattern>
    8. filter-mapping>

    1. import javax.servlet.ServletException;
    2. import javax.servlet.http.HttpServlet;
    3. import javax.servlet.http.HttpServletRequest;
    4. import javax.servlet.http.HttpServletResponse;
    5. import java.io.IOException;
    6. public class MyServlet extends HttpServlet {
    7. @Override
    8. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    9. String header = req.getHeader("My-Interceptor");
    10. resp.getWriter().write(header != null ? header : "Header Not Set");
    11. }
    12. @Override
    13. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    14. doGet(req, resp);
    15. }
    16. }

    文件上下载

    1. import java.io.File;
    2. import java.io.FileInputStream;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5. import java.net.HttpURLConnection;
    6. import java.net.URL;
    7. public class FileUploader {
    8. private static final String UPLOAD_URL = "http://example.com/upload"; // 上传文件的URL地址
    9. private static final String FILE_PATH = "path/to/file"; // 要上传的文件路径
    10. public static void main(String[] args) throws IOException {
    11. File file = new File(FILE_PATH);
    12. if (!file.exists()) {
    13. System.out.println("文件不存在");
    14. return;
    15. }
    16. URL url = new URL(UPLOAD_URL);
    17. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    18. conn.setRequestMethod("POST");
    19. conn.setDoOutput(true);
    20. conn.setRequestProperty("Content-Type", "application/octet-stream"); // 文件类型
    21. conn.setRequestProperty("filename", file.getName()); // 文件名
    22. InputStream in = new FileInputStream(file);
    23. byte[] buffer = new byte[1024];
    24. int len;
    25. while ((len = in.read(buffer)) != -1) {
    26. conn.getOutputStream().write(buffer, 0, len);
    27. }
    28. in.close();
    29. if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
    30. System.out.println("上传成功");
    31. } else {
    32. System.out.println("上传失败");
    33. }
    34. conn.disconnect();
    35. }
    36. }

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import java.io.InputStream;
    4. import java.net.HttpURLConnection;
    5. import java.net.URL;
    6. public class FileDownloader {
    7. private static final String DOWNLOAD_URL = "http://example.com/download"; // 下载文件的URL地址
    8. private static final String FILE_PATH = "path/to/save/file"; // 要保存的文件路径
    9. public static void main(String[] args) throws IOException {
    10. URL url = new URL(DOWNLOAD_URL);
    11. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    12. conn.setRequestMethod("GET");
    13. conn.setDoOutput(true);
    14. InputStream in = conn.getInputStream();
    15. FileOutputStream out = new FileOutputStream(FILE_PATH);
    16. byte[] buffer = new byte[1024];
    17. int len;
    18. while ((len = in.read(buffer)) != -1) {
    19. out.write(buffer, 0, len);
    20. }
    21. in.close();
    22. out.close();
    23. if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
    24. System.out.println("下载成功");
    25. } else {
    26. System.out.println("下载失败");
    27. }
    28. conn.disconnect();
    29. }
    30. }

  • 相关阅读:
    Linux —— 进程控制
    运维:mysql常用的服务器状态命令
    可视化—AntV G6 紧凑树实现节点与边动态样式、超过X条展示更多等实用小功能
    element-plus组件问题汇总
    SSM - Springboot - MyBatis-Plus 全栈体系(三十)
    事务(Transaction)逻辑应用
    JVM-虚拟机的故障处理与调优案例分析
    sql 行列互换
    信息化与信息系统4
    第十三届蓝桥杯省赛C++ C组《全题目+题解》
  • 原文地址:https://blog.csdn.net/NanCheng_666/article/details/132993516