Use Ajax to request a picture of the server and display it in the browser (GO)

Source: Internet
Author: User

Objective

Always in the database is stored in the image of the server address, and then to the browser display, but 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 BufferedImage this class.

Begin

First of all, my idea is to put the local picture, loaded into memory, and then put into the bufferedimage this buffer stream, and then use Imageio.write (), which everyone is estimated to want a train of thought, but if Ajax, get the data, Guess what a mess it is! 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 is the use of imageio.read, to load the stream object, and then the code to deal with the class, which I used is springmvc,springmvc this period of time compared to fire, so I also good less 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
            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, natural, 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 src attribute, you can get the picture.

Use Ajax to request a picture of the server and display it in the browser (GO)

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.