Java 正在尝试在cassandra数据库中以blob形式插入图像/xml文件

Java 正在尝试在cassandra数据库中以blob形式插入图像/xml文件,java,xml,cassandra,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;

实际上,我需要在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内容时遇到问题。谁能帮我一下吗。

  • 您正在将数据插入电子邮件并从另一封电子邮件中进行选择
  • 读取图像字节的更好方法是:

    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();
    

尝试使用datastax OGM库进行DB操作。此外,当你说“面临问题”时,请描述问题的具体内容,例如错误、堆栈trace@monish线程“main”java.lang.NullPointerException中的异常,我在重新保存记录时获取空值实际上我插入了一条记录,并根据插入的记录选择了该记录。