2015-04-20 27 views
0

我正在学习Cassandra并编写了一个restclient,它将从DB读取文件。cassandra行提取垃圾字符

以下是我从DB中检索数据的代码。

 ResultSet rs = getFile(fileName); 
     Row row=rs.one(); 

     ByteBuffer filecontent =row.getBytes("file_content"); 
     byte[] data = new byte[filecontent.remaining()]; 
     ByteBuffer bb = filecontent.get(data); 

     String filelocation = row.getString("file_location"); 
     String filename = row.getString("filename"); 

     ByteArrayInputStream filecontentfromDB= new ByteArrayInputStream(bb.array()); 
     File file=writeToFile(filecontentfromDB,fileName); 

     if (rs == null) { 
      return null; 
     } 
     return file; 

当我看到返回的文件时,我发现一些垃圾字符在开头,然后是我的文件内容。

请帮我去除垃圾数据

回答

0

Yipee,找到了解决办法,下面的线应及时更换

String filelocation = row.getString("file_location"); 
     String filename = row.getString("filename"); 

     ByteArrayInputStream filecontentfromDB= new ByteArrayInputStream(bb.array()); 

String filelocation = row.getString("file_location"); 
     String filename = row.getString("filename"); 
String bb = new String(filecontent.array(), filecontent.arrayOffset() + filecontent.position(), filecontent.remaining()); 
     ByteArrayInputStream filecontentfromDB= new ByteArrayInputStream(bb.getBytes()); 

它的工作原理。