java下载文件到本地 java下载文件


java下载文件到本地 java下载文件


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;

推荐阅读