緩存時間的設置:
1 public void doGet(HttpServletRequest request, HttpServletResponse response)2 throws ServletException, IOException {3 4 response.setContentType("text/html;charset=UTF-8");5 PRintWriter out = response.getWriter();6 response.setDateHeader("Expires", System.currentTimeMillis()+60*60*1000);//緩存時間一小時7 out.write("hello world");8 }通知客戶端文件的下載,此處以圖片為例:
1 public void doGet(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException { 3 response.setContentType("text/html"); 4 ServletContext context=getServletContext(); 5 6 String path=context.getRealPath("/picture.jpg"); 7 String fileName=path.substring(path.lastIndexOf("http://")+1); 8 InputStream in=new FileInputStream(path); 9 10 //通知客戶端文件的下載 URLEncoder.encode解決文件名中文的問題11 response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "utf-8"));12 response.setHeader("Content-Type", "application/octet-stream");13 14 OutputStream out=response.getOutputStream();15 int length=-1;16 byte []buffer=new byte[1024];17 while((length=in.read(buffer))!=-1){18 out.write(buffer,0,length);19 }20 in.close();21 }新聞熱點
疑難解答