Add a picture as a binary stream into the database----convert the picture into a binary stream

Source: Internet
Author: User

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

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.