尝试在cassandra数据库中将image / xml文件作为blob插入

时间:2017-02-21 05:36:26

标签: java xml cassandra

实际上我需要在cassandra数据库中插入xml文件。因此,最初我尝试将图像作为blob内容插入后,我将代码更改为插入xml但面临插入问题并将blob内容检索为图像。任何人都可以建议在cassandra数据库中插入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,"xxx@gmail.com","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("ramya1@gmail.com"));

    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内容检索为图像时遇到问题。有谁可以帮我这个。

1 个答案:

答案 0 :(得分: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();