java_method_下载导入模版

//调用方法

upDown.download(request, response, request.getRealPath("")+"/output/BlackNumberTemp.xls", "BlackNumberTemp.xls", "application/vnd.ms-excel");

//导入模版method

public boolean download(HttpServletRequest request,
HttpServletResponse response, String filePath, String strFileName,
String strType) {
try {
File f = new File(filePath);

response.reset();
response.setContentType( strType );//设置下载文件的类型
response.setHeader("content-disposition","attachment; filename="+strFileName); //设置下载的文件名

long fileLength=f.length();
String length1=String.valueOf(fileLength);
response.setHeader("Content_Length",length1); //下载文件的大小

InputStream in = new FileInputStream( f );
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[2097152];

int ins = in.read(buffer);//读取字节到buffer中

//ins == -1 时 。就已经是文件的结尾了
while ( ins != -1 ) {
out.write(buffer, 0, ins);//将缓存buffer中的数据写到文件中
ins = in.read(buffer);
}
in.close();
out.flush();
out.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}

}

原文地址:https://www.cnblogs.com/zxxbk/p/6255535.html