Settings for cache time:
1 Public voiddoget (httpservletrequest request, httpservletresponse response)2 throwsservletexception, IOException {3 4Response.setcontenttype ("Text/html;charset=utf-8");5PrintWriter out =Response.getwriter ();6Response.setdateheader ("Expires", System.currenttimemillis () +60*60*1000);//cache time of one hour7Out.write ("Hello World");8}
Notifies the client of the download of the file, taking the picture as an example:
1 Public voiddoget (httpservletrequest request, httpservletresponse response)2 throwsservletexception, IOException {3Response.setcontenttype ("text/html");4ServletContext context=Getservletcontext ();5 6String Path=context.getrealpath ("/picture.jpg");7String filename=path.substring (path.lastindexof ("\ \") +1);8InputStream in=NewFileInputStream (path);9 Ten //notifies the client file of the download Urlencoder.encode address the Chinese file name problem OneResponse.setheader ("Content-disposition", "attachment;filename=" +urlencoder.encode (filename, "Utf-8")); AResponse.setheader ("Content-type", "Application/octet-stream"); - -OutputStream out=Response.getoutputstream (); the intLength=-1; - byte[]buffer=New byte[1024]; - while(Length=in.read (buffer))!=-1){ -Out.write (buffer,0, length); + } - in.close (); +}
Servlet set cache time and file download