I am using mongodb community version. I stored pdf file with size less than 16 MB in table as follows
byte[] bytesArray = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bytesArray); //read file into bytes[]
fis.close();
document.append("binaryFile", new BsonBinary(bytesArray));
collection.insertOne(new Document(document));
But at the time of retrial of the pdf file from database I used following code
FindIterable it = collection.find(whereQuery);
ArrayList<Document> docs = new ArrayList<Document>();
it.into(docs);
for (Document doc1: docs) {
System.out.println(doc1);
byte[] c = ((org.bson.types.Binary)doc1.get("binaryFile")).getData();
if(c.length==0){
}else{
fileInputStream= new ByteArrayInputStream(c);
fileInputStream.read();
}
At the this time pdf file got created but could not be opened. It displayed following error.
“Error Adobe Reader could not open document.pdf becase it is either not a supported file or because file has damaged”.