"Java seconds will get the technology out of the way BLOB data type" how to read and display a picture of a BLOB type in a database

Source: Internet
Author: User
Tags base64 oracle database

how to read and display a picture of a BLOB type in a database

"Foreword" recently, when making a request, you need to read the picture from the Oracle database. This thought that the database store would be a simple URL, front can be directly displayed, the results are found to be BLOB binary type, as a result, Baidu/google keyword "binary picture reading and display", found that there are many "copy to copy" article or blog, but the quality of the article is very low, And the structure is more chaotic, after reading is still "unknown so".

Finally, spent nearly 2 hours, patiently studied a bit, finally succeeded. So summarized as follows, for your reference, also as the accumulation of their own backup use.

" Core Point "

1. Directly accept the BLOB type in the PO class using the byte[] type;

2. Convert binary into Base64 format string (Do not use Sun.misc.BASE64Encoder Class);

3. The foreground HTML page is displayed by tag.

1. Requirements Overview

① a blob-type picture from a table in an Oracle database;

② the picture on the front page.

2. Create PO class, in PO class with byte[] type directly accept BLOB type

Package Com.netease.numen.plus.netease.model;
Import java.io.Serializable;
/**
 * Read binary blob type picture
 * <p>Title:PhotoBytes</p>
 * @author  Liyan
 * @date    March 6, 2017 pm 5:02:22
 */Public
class Photobytes implements Serializable {
	private static final long Serialversionuid = -8775612234303127935l;
	/** binary Picture * *
	private byte[] photobytes;
	Public byte[] Getphotobytes () {return
		photobytes;
	}
	public void Setphotobytes (byte[] photobytes) {
		this.photobytes = photobytes;
	}	
}

3. Preparation of Mapper.xml documents

<?xml version= "1.0" encoding= "UTF-8"?> <!  
DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace= "Com.**.netease.mapper.workcardmapper" > <resultmap id=, photobytes "type=" com.**

	. Netease.model.PhotoBytes ">
		<result column=" PHOTO "property=" photobytes "/>
	</resultMap>

	<!--According to employee work number-->
	<select id= "Getphoto" resultmap= "photobytes" >
		select
			PHOTO as Photobytes
		from
			ehrinbound. Ehr_employee_photo_v
		<where>
			<if test= "Jobnumber!=null" > and
				ehr_employee_code = #{ Jobnumber} 
			</if>
		</where>
	</select>
	
</mapper>

4. Preparation of Mapper.java documents

@Repository Public
interface Workcardmapper {public
	photobytes Getphoto (map<string, object> param) Throws Exception;
}

5. Preparation of Service.java documents

Public interface Workcardservice {public	
	photobytes Getphoto (final String jobnumber) throws Exception;
}

6. Preparation of Serviceimpl.java documents

@Service ("Workcardservice") Public
class Workcardserviceimpl implements workcardservice{
	@Inject
	Workcardmapper mapper;
	@Override
	public  photobytes Getphoto (String jobnumber) throws Exception {
        map<string, object> param = new hashmap<string, object> (0);
	    Param.put ("Jobnumber", Jobnumber);
	}

7. Preparation binary Flow Base64 A tool class for strings

Package com.netease.numen.plus.netease.util;
Import java.io.IOException;
Import org.apache.commons.codec.binary.Base64;
public class Base64imageutil {
	/**
	 * Binary Flow Base64 string
	 * <p>Title:byteArr2String</p>
	 * Author Liyan
	 * @date   March 6, 2017 afternoon 3:04:47
	 * @param data
	 * @return String
	 * @throws ioexception< c15/>*/public
	static string bytearr2string (byte[] bytearr) throws Exception {
		string stringBase64 = Null;
  try {
			 Base64 encoder = new Base64 ();
			 StringBase64 = (Bytearr!= null? encoder.encodetostring (Bytearr): "");
		} catch (Exception e) {
			throw new Exception ("Bytearr2string conversion Exception:" +e);
		}
		Return stringBase64
    }
}

"Extension: Why not use the Sun.misc.BASE64Encoder class. "

Many people use Base64 for encryption and decryption, are using Sun.misc package Base64encoder and Base64decoder. However, this may be the case when the JDK is found without the hint to find the class.

There is a way to say reconfigure the JRE on the Internet. But the fact is not so simple, it is to see whether the type of JRE configured is execution environment (execution environment) or alternate JRE (instead of JRE).

In either case, if it's the first one, because it's an execution environment, or some extra class that doesn't contain JDK, many of the packages in Rt.jar will be restricted because the JRE and JDK permissions are different, including the sun package.

If this is the second method, that is, the JDK replaces the JRE, this is fine, because the permissions of the JDK are used, so no such errors are reported.

But, in fact, both of these methods are internal to sun and are not exposed in the Java API, so using these methods is unsafe and may be removed at any time in the future, so you should use the appropriate alternative objects and methods. "Reference: http://fableking.iteye.com/blog/1426410"

8. Preparation of Controller.java documents

The following is abbreviated, and the main step is to Base64 the byte[into the string and return it to the front end. Actually also need to add parameter check, exception processing, logging and other logic.

Package Com.netease.numen.plus.netease.controller;
@Controller
@RequestMapping (value = "/workcard") public
class Workcardcontroller {
	@Inject
	Workcardservice Service;
	
	@RequestMapping (value = "/getworkcardinfo") public
	String Getworkcardinfo (string jobnumber) {
		//According to employee work number, get photos ( Byte[] type)	
		photobytes photobytes = Service.getphoto (jobnumber);
		byte[] PhotoBytes2 = Photobytes.getphotobytes ();
		Converts binary to Base64 format string
		photo64 = base64imageutil.bytearr2string (photoBytes2);
		result = new Resultinf (200, "Get photo information successful", photo64);
		Springmvcutil.renderjson (response, result);
		return null;
	}
}

9 . Foreground HTML page display



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.