1. Convert a picture to a binary stream: done by FileInputStream
Public Static byte[] Change_to_stream (String path) {byte[] Imagebytes =NULL;
//Use Try-with-resourse directly here, you can not manually close the resources Try(FileInputStream FileInputStream =NewFileInputStream (NewFile (path));) {Imagebytes=New byte[Fileinputstream.available ()]; Fileinputstream.read (imagebytes); } Catch(IOException e) {e.printstacktrace (); } returnimagebytes; }
2. Output a binary stream to a picture
Public Static void change_to_image (byte[] imagebytes, String newpath) { //here directly with Try-with-resourse, You can not manually close the resource trynew fileoutputstream (new File (newpath));) { fileoutputstream.write (imagebytes); Catch (IOException e) { e.printstacktrace (); } }
3. Test can create a 2.txt directly on the desktop and then modify it to 2.jpg
public static void main (string[] args) throws IOException {String ImagePath = " c:\\users\\sj676\\desktop\\1.jpg " ; Source picture path String despath = c:\\users\\ Sj676\\desktop\\2.jpg ; Image output Destination path byte [] imagebytes = null ; Imagebytes = Change_to_stream (ImagePath); Convert the picture into a binary stream, and the source picture is all right after the conversion change_to_image (Imagebytes, Despath); Convert a binary stream to a picture}
4. Realization of the Vision:
By converting the image to binary, you can put the binary into the data and then take it out of the database and output it to the place you want to use it.
So the question is, how does the database exist in binary?
For your funeral, listen to tell.
Add a picture as a binary stream into the database----convert the picture into a binary stream