一、背景
最近做的一个项目,需要下载一个Excel模板,然后导入数据,所以,将Excel文件放到resources目录下。
二、方式
2.1 刚开始本地测试的使用的是如下方式:但是在服务器上却无法下载,因为项目打成jar包
try {
File sourceFile = ResourceUtils.getFile(Constants.EXCEL_TEMPLATE_PATH);
String filePath = sourceFile.getPath();
String realFileName = sourceFile.getName();
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition",
"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
FileUtils.writeBytes(filePath, response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
FileUtils工具类如下:
public class FileUtils {
public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
/**
* 输出指定文件的byte数组
*
* @param filePath 文件路径
* @param os 输出流
* @return
*/
public static void writeBytes(String filePath, OutputStream os) throws IOException {
FileInputStream fis = null;
try {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
}
fis = new FileInputStream(file);
byte[] b = new byte[1024];
int length;
while ((length = fis.read(b)) > 0) {
os.write(b, 0, length);
}
} catch (IOException e) {
throw e;
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
/**
* 删除文件
*
* @param filePath 文件
* @return
*/
public static boolean deleteFile(String filePath) {
boolean flag = false;
File file = new File(filePath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
/**
* 文件名称验证
*
* @param filename 文件名称
* @return true 正常 false 非法
*/
public static boolean isValidFilename(String filename) {
return filename.matches(FILENAME_PATTERN);
}
/**
* 下载文件名重新编码
*
* @param request 请求对象
* @param fileName 文件名
* @return 编码后的文件名
*/
public static String setFileDownloadHeader(HttpServletRequest request, String fileName)
throws UnsupportedEncodingException {
final String agent = request.getHeader("USER-AGENT");
String filename = fileName;
if (agent.contains("MSIE")) {
// IE浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
// 火狐浏览器
filename = new String(fileName.getBytes(), "ISO8859-1");
} else if (agent.contains("Chrome")) {
// google浏览器
filename = URLEncoder.encode(filename, "utf-8");
} else {
// 其它浏览器
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
/**
* 输出指定文件的byte数组
*
* @param filePath 文件路径
* @param content
* @return
*/
public static void writeStringFile(String filePath, String content) {
File file = new File(filePath);
try {
if (!file.exists()) {
file.createNewFile();
}
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
bw.write(content);
bw.flush();
} catch (Exception e) {
}
} catch (Exception e) {
}
}
}
2.2 后来改成如下方式获取,既可以在idea中获取,又可以在linux服务器上获取
try {
ClassPathResource classPathResource = new ClassPathResource(Constants.EXCEL_TEMPLATE_PATH);
InputStream inputStream = classPathResource.getInputStream();
//输出文件
InputStream fis = new BufferedInputStream(inputStream);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
//获取文件的名字再浏览器下载页面
String name = "filename";
response.addHeader("Content-Disposition", "attachment;filename=" + FileUtils.setFileDownloadHeader(request, name));
response.addHeader("Content-Length", "" + file.length());
OutputStream out = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
out.write(buffer);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}