【发布时间】:2015-07-03 06:32:10
【问题描述】:
我用 MongoDB 编写了一个 Restful 网络服务。这是我的代码:
@GET
@Path("/download/{id}/")
@Produces("application/epub")
public Response getImagefile(@PathParam("content") String content,
@PathParam("id") String imageID) throws UnknownHostException, IOException {
String urlContent = "D:\\NetbeansWorkspace\\Webservice\\web\\download\\image.jpg";
GridFSDBFile findImage;
DB db = getConnection().getDB(content);
GridFS gfsImage = new GridFS(db, "image");
findImage = gfsImage.findOne(new ObjectId(imageID));
findImage.writeTo(urlContent);
File file = new File(urlContent);
ResponseBuilder response = Response.ok((Object) urlContent);
response.header("Content-Disposition",
"attachment; filename=" + "image.jpg");
return response.build();
}
我正在从服务器中提取图像并提供下载链接。此代码通过先将其保存到 urlContent(我的本地文件)然后使其可供下载来实现这一点的效率并不高。我想跳过保存到 urlContent 部分并提供一个链接,该链接直接从服务器中提取和下载 blob 图像。我该怎么做?
【问题讨论】:
标签: java mongodb blob restful-url gridfs