/**
* @description: 从jar包内部的资源目录下下载文件
* @param relativePath-文件在资源目录下的相对路径,路径必须不以“/”开头。如“static/a.txt”
* @param resp
*/
public static void downLoadFromJar(String relativePath, HttpServletResponse resp,String encode) {
resp.setContentType("text/html;charset=" + encode);
InputStream input = null;
OutputStream output = null;
try {
input = FileUtil.class.getClassLoader().getResourceAsStream(relativePath);
resp.setHeader("Content-disposition", "attachment; filename=" + new String("附件标题.xlsx".getBytes(encode), "ISO8859-1"));
resp.setContentType("application/x-msdownload;");
output = resp.getOutputStream();
int count = 0;
byte[] by = new byte[1024];
while ((count = (input.read(by))) != -1) {
output.write(by, 0, count);
output.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (output != null) {
output.close();
}
if (input != null) {
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}