我使用Spring数据和GridFs模板从Mongo检索了图像
因此,我不知道如何将检索到的输入流返回给用户。
假设他们请求http://host.com/apple作为春季rest调用。现在,我的应用程序使用名称apple来处理请求,它从mongodb数据库中检索苹果图像。现在,在不保存任何地方的情况下,我希望将响应显示为用户将在浏览器中显示http://host.com/apple图像的图像。我到底需要如何实现这一点呢?
请您共享用于处理Rest调用中的图像请求的代码库吗?
Controller Code
@RestController
public class Greet
有一种简单的方法可以用gridFSDBFile做到这一点,但现在不再是了。
//This used to return a GridFSDBFile type. Returns GridFSFile now.
GridFSFile gridFsFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(idTemp)));
//And would be done this way. Not anymore.
gridFSFile.writeTo(outputstream)
主要目标是返回保存在数据库中的图像,
我正在使用GridFS来存储图像。现在我想把存储的图片直接写到spring视图页面,我已经尝试了很多,但没有成功。我可以使用以下命令将图像写入我的本地系统
gfs.writeTo("my location of local directory");
但是,在spring中如何将相同的图像写入JSP视图呢?任何帮助都是非常感谢的。
我使用的是Mongo-Java-Driver 2.13,我在GridFS中存储了一个PDF文件(大小为30 in )。我能够很容易地执行插入、删除和查找操作。
MongoClient mongo = new MongoClient("localhost", 27017);
DB db = mongo.getDB("testDB");
File pdfFile = new File("/home/dev/abc.pdf");
GridFS gfs = new GridFS(db,"books");
我正在为文章编写一个存储库,我已经决定使用来自MongoDB的MongoDB。我已经找到并测试了一些代码,但我不确定它是否是最好的代码。你能告诉我如何使它变得更好吗?
private GridFS gridFs;
public String getPostContent(String name) {
GridFSDBFile file = gridFs.findOne(name + ".html");
if (file == null) {
return "There is no text for this article.";
当我已经拥有了正确的GridFS时,我想不出如何使用spring及其GridFSTemplate从ObjectId中流出二进制文件。
GridFSTemplate返回GridFSResource (getResource())或GridFSFile (findX())。
我可以通过ID获得GridFSFile:
// no way to get the InputStream?
GridFSFile file = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(id)))
但是,没有明显的方法可以为这个In