Ajax clears the Javascript, css, and image caching methods of browsers,

Source: Internet
Author: User

Ajax clears the Javascript, css, and image caching methods of browsers,

The pictures are stored in the database at the server address and displayed in the browser. However, two problems were discovered later.

First, for security reasons, JavaScript cannot read local images. Otherwise, if you write a js file, wouldn't you be able to get files from anyone's computer.

Second, the image is stored on the server's hard disk, rather than in the customer's hard disk, so it cannot be obtained.

Later, I searched for methods on the Internet and found various methods to convert binary data into xml. I was too lazy and thought of one.

The method is to useBufferedImageThis class.

Start

First, let's talk about my idea: load the local image into the memory, and then put it inBufferedImageBuffer the stream and then useImageIO. write (),Now we all want to get a new idea, but if we say ajax and the data we get, we can guess it's a mess! It doesn't matter. I will introduce it later.

Tool

First, create a tool for loading an image and store the address of an image as a parameter to obtain the buffer stream of the image:

/*** Return the buffer stream of the Image Based on the image address * @ param addr * @ return */public static BufferedImage getInputStream (String addr) {try {String imgPath = addr; bufferedImage image = ImageIO. read (new FileInputStream (imgPath); return image;} catch (Exception e) {e. printStackTrace (); System. out. println (); System. out. println ("Image Retrieval exception: java. awt. image. bufferedImage "); System. out. println ("check whether the image path is correct or whether the address is an image");} return null ;}

That's right.ImageIO. readTo load the stream object, and then to process the class code. This is what I useSpringMVC,SpringMVCThis time

It's very popular, so I should have used struts2 less.

Processing class

/*** Obtain the Image Based on the image address * @ param addr * @ param response */@ ResponseBody @ RequestMapping ("/getImg ") public void getImg (@ Param ("addr") String addr, HttpServletResponse response) {BufferedImage img = new BufferedImage (300,150, BufferedImage. TYPE_INT_RGB); img = ImgUtil. getInputStream (addr); if (img = null) {throw new RuntimeException ("Print Image exception: com. controller. business_Ctrl.getImg (String, HttpServletResponse )") ;} If (img! = Null) {try {ImageIO. write (img, "JPEG", response. getOutputStream ();} catch (IOException e) {e. printStackTrace (); System. out. println ("Print exception: com. controller. business_Ctrl.getImg (String, HttpServletResponse )");}}}

ObviouslyImageIO. read ()When reading an image, useImageIO. write (),Output image. The input stream is

HttpServletResponse. getOutputStream ()

Client

 Copy codeThe Code is as follows:
Function setImg (addr ){
         
$ ("# Logo"). attr ("src", "business/getImg? Addr = "+ addr + "");
}

When an image needs to be loaded, the setImg method is triggered to give it an address. Of course, the address has already been transferred from the background to the foreground. Naturally, even if there is no address, change it a bit, you can also get the address in the background, return it, and set the src attribute for the img label to get the image.

The following describes how jquery1.2 uses Ajax to clear browser js, css, and image cache.

Jquery has been available since 1.2.IfModified and cacheYou do not need to add the header.

ifModified Boolean Default: false Allow the request to be successful only if the response has changed since the last request. This is done by checking the Last-Modified header. Default value is false, ignoring the header. cache Boolean Default: true Added in jQuery 1.2, if set to false it will force the pages that you request to not be cached by the browser. $.ajax({type: "GET",url: "static/cache.js",dataType: "text",cache:false,ifModified :true});

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.