The application of Redis in Spring Boot project (d) Reading and caching of the picture byte string

Source: Internet
Author: User

Picture because the file is relatively large, commonly used pictures such as reading the database or file to increase the system load. The use of caching methods can increase efficiency.

Http://stackoverflow.com/questions/13215024/weird-redis-key-with-spring-data-jedis here has provided a picture of the reading method and type conversion method, can be modified.

In this paper, the files under the project are read into the Redis cache cache database.

Read files and encoded pictures as strings

    public static String encodetostring (bufferedimage image, String type) {//Will <span style= font-family:arial, Helvet ICA, Sans-serif; "
        >bufferedimage conversion to String,type is file type: jpg,png, etc. </span> String imagestring = null;

        Bytearrayoutputstream BOS = new Bytearrayoutputstream ();
            try {imageio.write (image, type, BOS);

            byte[] imagebytes = Bos.tobytearray ();
            Base64encoder encoder = new Base64encoder ();

            imagestring = Encoder.encode (imagebytes);
        Bos.close ();
        catch (IOException e) {e.printstacktrace ();
    return imagestring;
    	* * * Read picture/public static string Readimage (String filename,string type) {//Incoming picture name and type, read file and return string <span style= "font-family:arial, Helvetica, Sans-serif;" >system.getproperty ("User.dir") is the root directory that gets the current project, target\\classes under Resource directory,</span> file File2 = new file (Syst Em.getproperty ("User.dir") + "\\target\\classes\\static\\image\\ "+filename);
		   	  try {bufferedimage img = imageio.read (file2);
		   	  BufferedImage newimg;
		   	  String Imagestr;
		   	  IMAGESTR = Encodetostring (img, type);
	    return imagestr;
	    catch (FileNotFoundException Ex1) {ex1.printstacktrace ();
	    catch (IOException Ex1) {ex1.printstacktrace ();
	  return null; }



The picture is stored in the Redis to get

Package Cn.edu.tju.redis;
Import Java.awt.image.BufferedImage;

Import Java.io.ByteArrayInputStream;
Import Javax.annotation.Resource;
Import Javax.imageio.ImageIO;
Import Sun.misc.BASE64Decoder;
Import Sun.misc.BASE64Encoder;
Import Org.springframework.data.redis.core.RedisTemplate;
Import Org.springframework.stereotype.Service; @Service public class Imageredisimpl implements Imageredis {@Resource private redistemplate<string, string> Redis  TemplateS; The Redis configuration refers to the previous article, @Override public void Save (string img, string name) {if (name!= null && img!= NULL &am p;& img.length () > 0 && name.length () > 0) {//Set key value saved in Redis String key = "photo."
			+name;
			String value= "";
			Value = new String (IMG);
		Redistemplates.opsforvalue (). Set (key, value); } else {System.out.println (Picture: There is a problem with the input parameter.)
		"); @Override public byte[] Get (String name) {if (name!= null) {string key = photo.  +name; Returns the byte array String value = Redistemp of a picture by passing in the filenameLates.opsforvalue (). get (key);
			byte[] x = decodetoimage (value);
		return x;
	return null;
	     public static byte[] Decodetoimage (String imagestring) {//decoding bufferedimage image = null;
	     byte[] Imagebyte = new byte[]{};
	         try {Base64decoder decoder = new Base64decoder ();
	         Imagebyte = Decoder.decodebuffer (imagestring);
return imagebyte;
Bytearrayinputstream bis = new Bytearrayinputstream (imagebyte);
Image = Imageio.read (bis);
	     Bis.close ();
	     catch (Exception e) {e.printstacktrace ();
	 return imagebyte;
 }

}


Related Article

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.