2017-02-21 77 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

实际上是插入一条记录并根据插入的记录选择记录。 –