2017-02-21 79 views
0

其實我需要在cassandra數據庫中插入xml文件。所以最初我試圖插入圖像作爲blob內容後來我更改代碼插入XML,但我面臨的問題插入和檢索blob內容爲圖像。任何人都可以建議在cassandra數據庫中插入image/xml文件的最佳做法。試圖在cassandra數據庫中將blob插入image/xml文件

FileInputStream fis=new FileInputStream("C:/Users/anand.png"); 
byte[] b= new byte[fis.available()+1]; 
int length=b.length; 
fis.read(b); 

System.out.println(length); 

ByteBuffer buffer =ByteBuffer.wrap(b); 

PreparedStatement ps = session.prepare("insert into usersimage (firstname,lastname,age,email,city,length,image) values(?,?,?,?,?,?,?)"); 

BoundStatement boundStatement = new BoundStatement(ps); 

int age=22; 
//System.out.println(buffer); 

session.execute(boundStatement.bind("xxx","D",age,"[email protected]","xxx",length,buffer)); 

//session.execute( boundStatement.bind(buffer, "Andy", length)); 



PreparedStatement ps1 = session.prepare("select * from usersimage where email =?"); 
    BoundStatement boundStatement1 = new BoundStatement(ps1); 
    ResultSet rs =session.execute(boundStatement1.bind("[email protected]")); 

    ByteBuffer bImage=null; 

    for (Row row : rs) { 
    bImage = row.getBytes("image") ; 
    length=row.getInt("length"); 
    } 

byte image[]= new byte[length]; 
image=Bytes.getArray(bImage); 

HttpServletResponse response = null; 
@SuppressWarnings("null") 
OutputStream out = response.getOutputStream(); 
response.setContentType("image/png"); 
response.setContentLength(image.length); 
out.write(image); 

我在將blob內容作爲圖像檢索時遇到問題。任何人都可以請幫助我。

+0

嘗試使用datastax OGM庫DB操作。另外當你說'面臨的問題',請描述究竟是什麼問題,如錯誤,堆棧跟蹤 –

+0

@monish線程「主要」java.lang.NullPointerException異常,獲取空值,同時回收記錄 –

回答

0
  • 您是插入數據,電子郵件和選擇其他;
  • 一種更好的方式來讀取圖像的字節數是:

    BufferedImage originalImage = ImageIO.read(new File("C:/Users/anand.png")); 
    ByteArrayOutputStream imageStream = new ByteArrayOutputStream(); 
    ImageIO.write(originalImage, "png", imageStream); 
    imageStream.flush(); 
    byte[] imageInByte = imageStream.toByteArray(); 
    
+0

實際上是插入一條記錄並根據插入的記錄選擇記錄。 –