1、FileUtil工具类
package com.cc.fastdfs.utils;
import org.springframework.core.io.ClassPathResource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
* @author cc
* @data 2021年05月19日 8:58
*/
public class FileUtil {
/**
* 下载项目根目录下doc下的文件
* @param response response
* @param fileName 文件名
* @return 返回结果 成功或者文件不存在
*/
public static String 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){
System.out.println("文件没有找到");
}
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) {
System.out.println("文件名转换编码出现问题:"+ 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();
System.out.println("系统找不到指定的文件:"+ e.getMessage());
return ("系统找不到指定的文件");
}catch (IOException e) {
e.printStackTrace();
System.out.println("Io流写入出现错误:"+ e.getMessage());
return "Io流写入出现错误";
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭流出现错误:"+ e.getMessage());
return "关闭流出现错误";
}
}
}
return "200";
}
}
2、调用示例
@RequestMapping(value = "downLoadExcel",produces = "application/octet-stream")
public void downLoadExcel(HttpServletResponse response){
String name = "综合评价上传模板.xlsx";
String s = FileUtil.downloadFile(response, name);
}
3、修改pom文件,解决乱码
<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>