Java implementation file conversion to binary storage and extraction

Source: Internet
Author: User

First, function Description:

The files are converted into binary data into the database, and when needed, they can be removed for installation and use.

Second, the database:

Create a database field to store the image into binary, this field has a requirement is to set the BLOB type of

[SQL]View Plaincopy
    1. CREATE TABLE ' save_image ' (
    2. ' ID ' int (a) is not NULL auto_increment,
    3. <span style="color: #FF0000;" > ' images ' blob</span>
    4. PRIMARY KEY (' id ')
    5. )

Third, convert the file into binary data and save the Java code:

[Java]View Plaincopy
  1. Public Void Save () throws SQLException
  2. {
  3. Connection=connectionmanager.getconn (); //Connect to the database and connect to your own database
  4. try {
  5. File file=new file ("d:\\1.jpg"); The file to convert
  6. FileInputStream inputstream=New FileInputStream (file);
  7. String sql="insert into save_image (images) VALUES (?)"; The SQL statements that are stored in the database must be executed with preparestatement
  8. Statement=connection.preparestatement (SQL);
  9. Statement.setbinarystream (1, InputStream, (int) file.length ());
  10. Statement.executeupdate ();
  11. } catch (FileNotFoundException e) {
  12. //TODO auto-generated catch block
  13. E.printstacktrace ();
  14. } catch (SQLException e) {
  15. //TODO auto-generated catch block
  16. E.printstacktrace ();
  17. }
  18. }

Four, take out the data and restore the file to the local Java code:

[Java]View Plaincopy
  1. Reading a database binary file
  2. Public void Readerjpg () throws SQLException
  3. {
  4. Connection=connectionmanager.getconn ();  //Connect to your own database!!!!!!!!
  5. String sqlstring="Select images from Save_image where id=4"; Read the binary code from the database to restore the file, here I read my own database ID 4 file
  6. File file=new file ("e:\\1.jpg"); Locally generated files
  7. if (!file.exists ())
  8. {
  9. try {
  10. File.createnewfile ();
  11. } catch (Exception e) {
  12. E.printstacktrace ();
  13. }
  14. }
  15. try {
  16. byte[] Buffer = new byte[4096*5];
  17. Statement=connection.preparestatement (sqlString);
  18. ResultSet = Statement.executequery ();
  19. if (Resultset.next ())
  20. {
  21. FileOutputStream outputstream = new FileOutputStream (file);
  22. InputStream IStream = Resultset.getbinarystream ("Images"); Go to field with Getbinarystream ()
  23. int size=0;
  24. While ((Size=istream.read (Buffer))!=-1)
  25. {
  26. SYSTEM.OUT.PRINTLN (size);
  27. Outputstream.write (Buffer,0,size);
  28. }
  29. }
  30. } catch (Exception e) {
  31. E.printstacktrace ();
  32. }
  33. }

Java implementation file conversion to binary storage and extraction

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.