Ajax Erase Browser JS, CSS, image caching methods

Source: Internet
Author: User

Do things when the picture in the server's address in the database, and then to the browser display, but later found two problems.

First: For the sake of security,JS is unable to read the local picture, or you write a JS, it is not possible to get anyone's computer inside the file.

Second: The picture exists in the server's hard disk, not in the customer's hard disk, so it is not available

Later on the Internet to find methods, find the method, are all kinds of conversion binary into XML, the various tall on the answer, and then I was too lazy, I think of a

method is to use the BufferedImage class.

Begin

First of all, my idea is to put the local picture, loaded into memory, and then put into the buffer stream bufferedimage , and then use imageio.write (), which everyone is estimated to want a train of thought, but if said Ajax, get the data, estimate or what a mess of what it! It doesn't matter, we'll introduce later.

Tool class

First set up a load of pictures of the tool, the address of a picture as a parameter, to get the buffer stream of this picture:

/**   * Depending on the address of the picture, return the buffer stream of the picture   * @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 ("Get Picture exception: Java.awt.image.BufferedImage");      System.out.println ("Please check that the picture path is correct, or if the address is a picture");    }    return null;  }

  

Yes, it's using imageio.readto load the stream object, and then the code that handles the class, which I use springmvc,springmvc During this time

Compare the fire, so I have less to use struts2

Processing class

/**   * According to the image's address, to obtain the picture   * @param addr   * @param response * *  /@ResponseBody @RequestMapping ("/ Getimg ") Public  void Getimg (@Param (" addr ") String Addr,httpservletresponse response) {    bufferedimage img = new BufferedImage (BUFFEREDIMAGE.TYPE_INT_RGB);    img = imgutil.getinputstream (addr);    if (img==null) {      throw new RuntimeException ("Print picture 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)");}}}  

  

Obviously, using imageio.read () to read the picture, use imageio.write (), output the image, the input stream is

Httpservletresponse.getoutputstream ()

Client

The code is as follows:
function setimg (addr) {                $ ("#logo"). attr ("src", "business/getimg?addr=" +addr+ "");            }

  



, when the need to load the picture, the trigger Setimg method, give it an address, of course, the address, has been from backstage to the foreground, naturally, even if there is no address, a little change, you can get the address in the background, and then return, and then to the img tag set the SRC attribute, You can get the picture.

The following is a jquery1.2 version of the use of Ajax to clear the browser JS,CSS, image caching methods.

jquery has ifmodified and cache parameters since 1.2, so you don't have to add headers.

Ifmodified Boolean Default:falseallow The request to being successful only if the response have changed since the last Reques T. checking the last-modified header. Default value is false, ignoring the header. Cache Boolean default:trueadded in JQuery 1.2, if set to false it would force the pages so you request to not be cached By the Browser.$.ajax ({type: "GET", url: "Static/cache.js", DataType: "Text", cache:false,ifmodified:true});

  


Ajax Erase Browser JS, CSS, image caching methods

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.