@获取resource下的文件
java获取resources下的文件流
public void downLoad(String type, HttpServletResponse response, HttpServletRequest request) throws IOException {
logger.debug("进入download");
String fileName = ExcelModelDownEnum.valueOf(type).getValue();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/excel/" + fileName);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(fileName.getBytes("utf-8"), "ISO8859-1"));
bis = new BufferedInputStream(inputStream);
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
logger.debug("完成download");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
bos.close();
}
if (bis != null) {
bis.close();
}
}
}