public static GeneralCacheAdministrator cacheAdmin = new GeneralCacheAdministrator();
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String path = getServletContext().getRealPath("/");
File file = null;
SimpleDateFormat sdf= new SimpleDateFormat("hh-mm-ss");
try {
file = (File)cacheAdmin.getFromCache(sdf.format(new Date()));
System.out.println("来自缓存!"+ sdf.format(new Date()));
} catch (NeedsRefreshException e) {
file = new File(path+"xmls\\Pipe11.xml");
cacheAdmin.putInCache(sdf.format(new Date()), file);
System.out.println("--缓存没有!"+sdf.format(new Date()));
}
sendResponse(file,response);
return;
}
/**
* 把文件用响应流写出
* @param file
* @param response
* @throws IOException
*/
public void sendResponse(File file,HttpServletResponse response) throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
byte[] input = new byte[1024];
boolean eof = false;
while (!eof) {
int length = bis.read(input);
if (length == -1) {
eof = true;
}
else {
bos.write(input, 0, length);
}
}
bos.flush();
bis.close();
bos.close();
}
}