Javaweb Learning Record (i)--response response header cache setting and download function implementation

Source: Internet
Author: User
Tags apache tomcat

I. Common response headers in HTTP

    • location:http://www.it315.org/index.jsp
    • Server:apache Tomcat
    • Content-encoding:gzip
    • Content-length:80
    • Content-language:zh-cn
    • content-type:text/html; charset=gb2312
    • Last-modified:tue, Jul 18:23:51 GMT
    • refresh:1;url=http://www.it315.org
    • Content-disposition:attachment; Filename=aaa.zip
    • Transfer-encoding:chunked
    • SET-COOKIE:SS=Q0=5LB_NQ; Path=/search
    • etag:w/"7777-1242234904000"
    • Expires:-1
    • Cache-control:no-cache
    • Pragma:no-cache
    • Connection:close/keep-alive
    • Date:tue, Jul 18:23:51 GMT

Second, set the cache information

public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
System.out.println ("-----------cacheservlet-------------");
Set the appropriate header information
Set cache Time 100 seconds
Response.setdateheader ("Expires",
System.currenttimemillis () +100*1000);
Disable the use of caching
Response.setdateheader ("Expires", 0);
Response.setheader ("Cache-control", "No-cache");
Response.setheader ("Pragma", "No-cache");

Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ");
Out.println ("<HTML>");
Out.println ("Out.println ("<BODY>");
Read file
String path = Getservletcontext (). Getrealpath ("/a.txt");
FileReader reader = new FileReader (path);
Char buffer[] = new char[256];
int len = 0;
while (len = reader.read (buffer))! =-1) {
Out.println (new String (buffer, 0, Len));
}
Reader.close ();
Out.println ("</BODY>");
Out.println ("</HTML>");
Out.flush ();
Out.close ();
}

/**
* Time of last modification
*/
@Override
Protected long getlastmodified (HttpServletRequest req) {
String path = Getservletcontext (). Getrealpath ("/a.txt");
File File = new file (path);
return file.lastmodified ();
}

A.txt File Contents:

A.txt placement address in the project:

Results:

Third, the download function source code is as follows

public class Downservlet extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
String path = Request.getservletcontext (). Getrealpath ("/down/China. png");
File File = new file (path);
Download the way to open this operation (Specify the encoding method, the download file name is consistent with the source file)
Response.AddHeader ("Content-disposition", "Attachment;filename="
+ Urlencoder.encode (File.getname (), "UTF-8"));
OutputStream OS = Response.getoutputstream ();
InputStream is = new FileInputStream (file);
int len = 0;
byte[] buffer = new byte[1024];
while (len = is.read (buffer))! =-1) {
Os.write (buffer, 0, Len);
}
Is.close ();
Os.close ();
}

public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}

}

The address of the downloaded file in this program is placed in the following location of the project:

Javaweb Learning Record (i)--response response header cache setting and download function implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.