Springboot下载静态资源excel模板到本地
1.控制层:/*** 下载文件清单模板* @param response* @return*/@GetMapping(value = "DownLoadExcel.do",produces = "application/octet-stream")public void downLoadExcel(HttpServletResp...
·
1.控制层:
/**
* 下载文件清单模板
* @param response
* @return
*/
@GetMapping(value = "DownLoadExcel.do",produces = "application/octet-stream")
public void downLoadExcel(HttpServletResponse response){
//模板名称
String fileName = "补录模板.xlsx";
ResponseResult result = FileUtil.downloadFile(response, fileName);
if(result.getCode() == "200" || "200".equals(result.getCode()) ){
response.setStatus(200);
log.info("补录模板下载成功");
} else {
response.setStatus(500);
log.error(result.getMessage());
}
}
2.工具类
@Slf4j
public class FileUtil {
/**
* 下载项目根目录下doc下的文件
* @param response response
* @param fileName 文件名
* @return 返回结果 成功或者文件不存在
*/
public static ResponseResult downloadFile(HttpServletResponse response, String fileName) {
InputStream stream = FileUtil.class.getClassLoader().getResourceAsStream("static/excel/" + fileName);
/*InputStream stream = null;
try {
stream = new ClassPathResource("excel/" + fileName).getInputStream();
} catch (IOException e) {
e.printStackTrace();
}*/
if(stream == null){
log.error("文件没有找到");
}
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
try {
String name = java.net.URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLDecoder.decode(name, "ISO-8859-1") );
} catch (UnsupportedEncodingException e) {
log.error("文件名转换编码出现问题:"+ e.getMessage());
}
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = response.getOutputStream();
bis = new BufferedInputStream(stream);
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
log.error("系统找不到指定的文件:"+ e.getMessage());
return new ResponseResult("500","系统找不到指定的文件");
}catch (IOException e) {
e.printStackTrace();
log.error("Io流写入出现错误:"+ e.getMessage());
return new ResponseResult("500","Io流写入出现错误");
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
log.error("关闭流出现错误:"+ e.getMessage());
return new ResponseResult("500","关闭流出现错误");
}
}
}
return new ResponseResult("200");
}
3.项目路径:
4.解决excel下载模板中出现的乱码问题
修改pom.xml依赖文件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.yml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.yml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.xlsx</exclude>
<exclude>**/*.xls</exclude>
</excludes>
</resource>
<resource>
<!--不校验表格-->
<directory>src/main/resources/</directory>
<filtering>false</filtering>
<includes>
<include>**/*.xlsx</include>
<include>**/*.xls</include>
</includes>
</resource>
</resources>
</build>

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)