本地创建文件目录及文件
配置文件
excel.download=D://DataShow_IMAGE//excel
@Value("${excel.download}")
private String excelPath; //配置文件下载地址地址
//excel模板下载 excelName文件名
@Override
public void downLoadExcel(String excelName,HttpServletResponse response) {
response.setCharacterEncoding("UTF-8");
File file=new File(excelPath+"/"+excelName);
response.setContentType("text/html;charset=UTF-8");
try {
//文件名excelName如果有空格,有的浏览器会下载失败
response.setHeader("Content-disposition","attachment;filename="+new String(excelName.getBytes(),"iso-8859-1"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
OutputStream os = null;
FileInputStream fileInputStream = null;
DataInputStream dataInputStream = null;
try {
os = response.getOutputStream();
fileInputStream = new FileInputStream(file);
dataInputStream = new DataInputStream(fileInputStream);
int r;
while((r=dataInputStream.read())!=-1) {
os.write(r);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fileInputStream.close();
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}