拦截器(Interceptor)是一种常用的设计模式,在软件工程领域被广泛应用。拦截器通常被用来处理各种请求和响应,可以在请求发送前或响应返回后进行一系列的操作和处理。拦截器可以用于请求身份认证、日志记录、性能优化、权限控制、数据加密等等,是很多框架和中间件中常用的一种组件。
在Java中,拦截器一般是通过实现javax.servlet.Filter接口或Spring框架中的HandlerInterceptor接口来实现的。拦截器可以被用于Web应用程序、消息队列、RPC调用等各种情况。
- import javax.servlet.*;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- public class MyInterceptor implements Filter {
- @Override
- public void init(FilterConfig filterConfig) throws ServletException {
- // 初始化操作,可以为空
- }
-
- @Override
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
- HttpServletRequest httpRequest = (HttpServletRequest) request;
- HttpServletResponse httpResponse = (HttpServletResponse) response;
-
- // 在此处可以进行各种拦截操作,例如身份认证、权限控制、日志记录等等
- // 这里只是一个简单的示例:在请求头中添加一个自定义的字段
- httpResponse.setHeader("My-Interceptor", "Hello World!");
-
- chain.doFilter(request, response);
- }
-
- @Override
- public void destroy() {
- // 销毁操作,可以为空
- }
- }
-
- <filter>
- <filter-name>MyInterceptorfilter-name>
- <filter-class>com.example.MyInterceptorfilter-class>
- filter>
-
- <filter-mapping>
- <filter-name>MyInterceptorfilter-name>
- <url-pattern>*url-pattern>
- filter-mapping>
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- public class MyServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- String header = req.getHeader("My-Interceptor");
- resp.getWriter().write(header != null ? header : "Header Not Set");
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req, resp);
- }
- }
-
文件上下载
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
-
- public class FileUploader {
- private static final String UPLOAD_URL = "http://example.com/upload"; // 上传文件的URL地址
- private static final String FILE_PATH = "path/to/file"; // 要上传的文件路径
-
- public static void main(String[] args) throws IOException {
- File file = new File(FILE_PATH);
- if (!file.exists()) {
- System.out.println("文件不存在");
- return;
- }
-
- URL url = new URL(UPLOAD_URL);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
- conn.setRequestMethod("POST");
- conn.setDoOutput(true);
- conn.setRequestProperty("Content-Type", "application/octet-stream"); // 文件类型
- conn.setRequestProperty("filename", file.getName()); // 文件名
-
- InputStream in = new FileInputStream(file);
- byte[] buffer = new byte[1024];
- int len;
- while ((len = in.read(buffer)) != -1) {
- conn.getOutputStream().write(buffer, 0, len);
- }
- in.close();
-
- if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
- System.out.println("上传成功");
- } else {
- System.out.println("上传失败");
- }
- conn.disconnect();
- }
- }
-
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
-
- public class FileDownloader {
- private static final String DOWNLOAD_URL = "http://example.com/download"; // 下载文件的URL地址
- private static final String FILE_PATH = "path/to/save/file"; // 要保存的文件路径
-
- public static void main(String[] args) throws IOException {
- URL url = new URL(DOWNLOAD_URL);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
- conn.setRequestMethod("GET");
- conn.setDoOutput(true);
-
- InputStream in = conn.getInputStream();
- FileOutputStream out = new FileOutputStream(FILE_PATH);
- byte[] buffer = new byte[1024];
- int len;
- while ((len = in.read(buffer)) != -1) {
- out.write(buffer, 0, len);
- }
- in.close();
- out.close();
-
- if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
- System.out.println("下载成功");
- } else {
- System.out.println("下载失败");
- }
- conn.disconnect();
- }
- }
-