Home > Back-end >  Using GridFsTemplate, according to the _id read from file, type error, can't find the reason
Using GridFsTemplate, according to the _id read from file, type error, can't find the reason

Time:09-23

 

The import com. Mongo. Client. Gridfs. GridFSBucket;
The import com. Mongo. Client. Gridfs. GridFSDownloadStream;
The import com. Mongo. Client. Gridfs. Model. GridFSFile;
import org.apache.commons.io.IOUtils;
The import org. Bson. Types. The ObjectId;
import org.junit.Test;
The import org. Junit. Runner. RunWith;
The import org. Springframework. Beans. Factory. The annotation. Autowired;
The import org. Springframework. Boot. Test. The context. SpringBootTest;
The import org. Springframework. Data. The mongo. Core. Query. The Criteria;
The import org. Springframework. Data. The mongo. Core. Query. The query;
The import org. Springframework. Data. The mongo. Gridfs. GridFsResource;
The import org. Springframework. Data. The mongo. Gridfs. GridFsTemplate;
The import org. Springframework. Test. The context. Takeup. SpringRunner;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@ SpringBootTest
@ RunWith (SpringRunner class)
Public class GridFsTest {

The @autowired
Private GridFsTemplate GridFsTemplate;

The @autowired
Private GridFSBucket GridFSBucket;



//take file
@ Test
Public void queryFile () throws IOException {

//according to the id file
GridFSFile GridFSFile=gridFsTemplate. FindOne (Query. The Query (Criteria) where (" _id ") is (" 5 ec8cea13cc2be46e01a0e65 ")));

//open a download stream objects
GridFSDownloadStream GridFSDownloadStream=gridFSBucket. OpenDownloadStream (gridFSFile getObjectId ());
//create GridFsResource object, obtain flow
GridFsResource GridFsResource=new GridFsResource (gridFSFile gridFSDownloadStream);
//data from flow
The String content=IOUtils. ToString (gridFsResource getInputStream (), "utf-8");
System.out.println(content);


}


}

An error prompt: org. Bson. BsonInvalidOperationException: the Value expected to be of type DOCUMENT is of unexpected type STRING
  • Related