导出文件可参考:
private void export(byte[] bytes, HttpServletResponse response) {
OutputStream os = null;
try {
// 设置文件ContentType类型为word
response.setContentType("application/octet-stream");
response.setHeader("content-type", "application/octet-stream");
// 设置文件头:设置下载文件名(encode防止非法字符)
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8") + ".docx");
response.setHeader("Content-Length", String.valueOf(bytes.length));
os = response.getOutputStream();
os.write(bytes);
os.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
静态文件配置参考:
在application.properties中配置:
// 设置访问前缀
spring.mvc.static-path-pattern=/file/**
// 设置静态文件目录
spring.resources.static-locations=file:upload/
访问地址:http://ip:port:file
其代理的静态目录为项目根路径下的upload文件夹: