Filter输出静态资源文件
应用场景:前后端分离时,开启该Filter前后端同时支持~
@Bean(name = "staticHtmlFilter")
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new StaticHtmlFilter());
bean.addUrlPatterns("/*");
return bean;
}import org.springframework.core.io.ClassPathResource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class StaticHtmlFilter implements Filter {
private List<String> suffixList = new ArrayList<>();
private int type = 0;
private String basePath = "/html";
private String physicalPath = "";
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.suffixList.add(".html");
this.suffixList.add(".js");
this.suffixList.add(".css");
this.type = 1;
this.basePath = "/html";
this.physicalPath = "";
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if (type == 0) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String contextPath = request.getContextPath();
String uri = request.getRequestURI();
for (String suffix : suffixList) {
if (uri.endsWith(suffix)) {
uri = uri.substring(contextPath.length(), uri.length());
InputStream inputStream = null;
if (type == 1) {
inputStream = getInputStreamByResources(basePath + uri);
} else if (type == 2) {
inputStream = getInputStreamByFile(physicalPath + uri);
}
if (inputStream != null) {
response.setCharacterEncoding("UTF-8");
if (".html".equals(suffix)) {
response.setContentType("text/html");
} else if (".css".equals(suffix)) {
response.setContentType("text/css; charset=UTF-8");
} else if (".js".equals(suffix)) {
response.setContentType("text; charset=UTF-8");
}
OutputStream outputStream = response.getOutputStream();
byte[] bytes = new byte[2048];
int r = -1;
while ((r = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, r);
}
outputStream.flush();
return;
}
}
}
filterChain.doFilter(servletRequest, servletResponse);
}
/**
* @param filePath
* @return
* @throws IOException
*/
private InputStream getInputStreamByFile(String filePath) throws IOException {
return new FileInputStream(filePath);
}
/**
* @param classpath
* @return
* @throws IOException
*/
private InputStream getInputStreamByResources(String classpath) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(classpath);
InputStream inputStream = classPathResource.getInputStream();
return inputStream;
}
@Override
public void destroy() {
}
}
实现文件下载功能:import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
推荐阅读
- Excel的文件图标打不开了
- 手机乱下载应用怎么办
- 苹果手机怎么查我之前下载过什么软件
- pr制作延时摄影教程
- 抖音卸载了从新下载能恢复原样吗
- 文件夹怎么设置密码
- 第一创业证券交易app下载 第一创业证券交易软件下载
- 百度网盘的文件怎么下载到电脑
- 绘画app下载 绘画app
- 苹果如何下载软件