2010-01-30 43 views
1

我已經將圖像添加到MySql數據庫中。我試圖通過使用java創建一個新文件從數據庫中獲取圖像。問題是一切正常,圖像文件已經創建,但圖像文件不包含圖像,它什麼也沒有,並且新創建的圖像文件的大小不同於原始圖像..... Sql代碼:我們可以使用java從MySql數據庫獲取添加的圖像嗎?

CREATE TABLE `pictures` (`id` int(11) NOT NULL auto_increment, `description` varchar(20) default NULL, `photo` blob, PRIMARY KEY (`id`)); 
insert into pictures values('1','pic1','D:\java.jpg'); 

和Java代碼:

public class ReadBlobPicture 
{ 
    static String url = "jdbc:mysql://localhost:3306/imgdatabase"; 
    static String username = "root"; 
    static String password = "tiger"; 

    public static void main(String[] args) 
     throws Exception 
    { 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection conn = DriverManager.getConnection(url, username, password); 
     String sql = "SELECT photo FROM pictures where id=1"; 
     Statement stmt = conn.prepareStatement(sql); 
     ResultSet resultSet = stmt.executeQuery(sql); 
     Object obj = resultSet; 

     while (resultSet.next()) 
     { 
      File image = new File("E:\\java12.jpg"); 
      FileOutputStream fos = new FileOutputStream(image); 
      System.out.println(resultSet.getString(1)); 
      byte[] buffer = new byte[1]; 
      InputStream is2 = resultSet.getBinaryStream(1); 
      System.out.println(is2.available()); 

      while (is2.read() > 0) 
      { 
       fos.write(buffer); 
       fos.flush(); 
      } 

      fos.close(); 
     } 

     conn.close(); 
    } 
} 

回答

0

你寫你的OutputStream一個問題,它應該是這樣的:

while ((buffer[0] = is2.read()) > 0) 
      { 
       fos.write(buffer[0]); 
       fos.flush(); 
      } 

而且你的照片應該被保存在數據庫中的BLOB!

P.S.你的緩衝區是無用的,因爲它的大小爲1,你應該使它至少512或1024,以逐字節讀取無效。

希望它有幫助。

+0

如何將字節數組轉換爲int.read()返回int。 – arindam 2010-01-30 07:55:58

0
insert into pictures values('1','pic1','D:\java.jpg'); 

不二進制數據JPG插入到數據庫中,它插入字符串D:\java.jpg

+0

那麼什麼是出路? – arindam 2010-01-30 07:53:07

1

你從流中讀取,但從來沒有讀取字符保存到緩衝區...

while (is2.read() > 0) 
{ 
    fos.write(buffer); 
    fos.flush(); 
} 

類似:

int x; 

while((x = is2.read()) != -1) 
{ 
    fos.write(x); 
} 

fos.flush(); 

(編輯 - 從另一個答案的評論.. )

對於未插入圖像的插入語句,請參閱this link

/* 

Defining the Table: Oracle and MySql 

create table MyPictures (
    id INT PRIMARY KEY, 
    name VARCHAR(0), 
    photo BLOB 
); 
*/ 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.SQLException; 

public class InsertPictureToMySql { 
    public static void main(String[] args) throws Exception, IOException, SQLException { 
    Class.forName("org.gjt.mm.mysql.Driver"); 
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root"); 
    String INSERT_PICTURE = "insert into MyPictures(id, name, photo) values (?, ?, ?)"; 

    FileInputStream fis = null; 
    PreparedStatement ps = null; 
    try { 
     conn.setAutoCommit(false); 
     File file = new File("myPhoto.png"); 
     fis = new FileInputStream(file); 
     ps = conn.prepareStatement(INSERT_PICTURE); 
     ps.setString(1, "001"); 
     ps.setString(2, "name"); 
     ps.setBinaryStream(3, fis, (int) file.length()); 
     ps.executeUpdate(); 
     conn.commit(); 
    } finally { 
     ps.close(); 
     fis.close(); 
    } 
    } 
} 
+0

感謝它的工作。但如果我從sql瀏覽器添加它將是什麼代碼? – arindam 2010-01-30 08:17:13

0
// create a file called MySqlInsertBlob.java 
//==========import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.PreparedStatement; 
import java.sql.Statement; 
import java.sql.Blob; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.*; 

class MySqlInsertBlob { 
FileOutputStream image; 
Connection conn = null; 
PreparedStatement pstmt = null; 
Statement stmt= null; 
ResultSet res = null; 
StringBuffer query=null; 
String filename ="c:/backblue.gif"; 

public MySqlInsertBlob(){ 

try{ 

query=new StringBuffer("insert into blobs(filename,binarydata,name) values (?,?,?)"); 
File file= new File(filename); 

Class.forName("com.mysql.jdbc.Driver")… 
conn = DriverManager.getConnection("jdbc:mysql:… 
stmt=conn.createStatement(); 
pstmt=conn.prepareStatement(query.toSt… 
pstmt.setString(1, filename); 
pstmt.setBinaryStream(2, new FileInputStream(filename),(int)file.leng… 
pstmt.setString(3,"silviya"); 
pstmt.executeUpdate(); 
System.out.println("Successfully inserted into BLOBS ....."); 

ResultSet rs=stmt.executeQuery("select * from blobs where name='silviya'"); 
if (rs.next()) { 
Blob test=rs.getBlob("binarydata"); 
InputStream x=test.getBinaryStream(); 
int size=x.available(); 
OutputStream out=new FileOutputStream("c:/xyz.gif"); 
byte b[]= new byte[size]; 
x.read(b); 
out.write(b); 


} 
}catch(ClassNotFoundException cnfe){ 

System.out.println("ClassNotFoundExcep… occured :"+cnfe); 
}catch(SQLException se){ 

System.out.println("SQLException occured :"+se); 
}catch(FileNotFoundException fe){ 

System.out.println("File Not Found Exception occured :"+fe); 
}catch(Exception e){ 

System.out.println("Exception occured :"+e); 
}finally{ 
try{ 
stmt.close(); 
conn.close(); 
}catch(Exception e){} 
} 
Source(s): 
+0

你剛剛發佈了很多代碼,沒有任何解釋和代碼中的一些評論。你可以給你的文章添加一些解釋嗎? – 2012-11-09 16:31:54

相關問題