日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java mongodb gridfs_MongoDB-4 GridFS 文件存储

發(fā)布時(shí)間:2024/3/13 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java mongodb gridfs_MongoDB-4 GridFS 文件存储 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 配置config

spring:

data:

mongodb:

uri: mongodb://username:password@192.168.2.72:27017

database: mydb

@Component

public class WebConfig {

@Value("${spring.data.mongodb.database}")

private String mongodb;

@Bean

public GridFSBucket getGridFSBucket(MongoClient mongoClient){

MongoDatabase database = mongoClient.getDatabase(mongodb);

GridFSBucket bucket = GridFSBuckets.create(database);

return bucket;

}

}

2.?基于gridfs的上傳,讀取與下載

@Autowired

private GridFSBucket gridFSBucket;

/**

* 創(chuàng)建文件到GirdFS

* @param model

* @return

* @throws Exception

*/

@GetMapping("/addToGridFS")

@ResponseBody

public Object addToGirdFS(Model model) throws Exception {

// 聲明freemarker的配置類

Configuration cfg = new Configuration(Configuration.getVersion());

// 聲明freemarker模板所需要加載的目錄位置

String classpath = this.getClass().getResource("/").getPath();

cfg.setDirectoryForTemplateLoading(new File(classpath + "templates"));

// System.out.println(classpath + "templates");

// 構(gòu)建數(shù)據(jù)

model = makeModel(model);

// 加載ftl模板

Template template = cfg.getTemplate("user.ftl", "utf-8");

// 獲得靜態(tài)化后的內(nèi)容

String htmlContent = FreeMarkerTemplateUtils.processTemplateIntoString(template, model.asMap());

InputStream inputStream = IOUtils.toInputStream(htmlContent);

// 上傳

ObjectId fileId = gridFSBucket.uploadFromStream("1001.html", inputStream);

System.out.println("上傳完成。 文件ID:" + fileId);

// 文件在mongodb中的id

return fileId.toString();

}

/**

* 讀取文件

* @param model

* @return

* @throws Exception

*/

@GetMapping("/readGridFS")

@ResponseBody

public String readGridFS(Model model) throws Exception {

// 獲取文件ID

String objectId = "5e42924980fb940ab75f141a"; // 根據(jù)upload后文件ID修改

// 獲取內(nèi)容

GridFSFindIterable gridFSFindIterable = gridFSBucket.find(Filters.eq("_id", new ObjectId(objectId)));

GridFSFile gridFSFile = gridFSFindIterable.first();

System.out.println("filename: " + gridFSFile.getFilename());

return gridFSFile.getFilename();

}

/**

* 從gridfs中下載文件

* @throws Exception

*/

@GetMapping("/downloadGridFS")

@ResponseBody

public String downloadGridFS() throws Exception {

// 獲取文件ID

String objectId = "5e42924980fb940ab75f141a";

// 獲取文件流,定義存放位置和名稱

File file = new File(htmlTarget + "/1001.html");

// 創(chuàng)建輸出流

OutputStream os = new FileOutputStream(file);

// 執(zhí)行下載

gridFSBucket.downloadToStream(new ObjectId(objectId), os);

System.out.println("下載成功");

return "ok";

}

@GetMapping("/downloadGridFSByFileName")

@ResponseBody

public String downloadGridFSByFileName() throws Exception {

// 獲取文件ID

String filename = "1001.html";

// 獲取文件流,定義存放位置和名稱

File file = new File(htmlTarget + "/" + filename);

// 創(chuàng)建輸出流

OutputStream os = new FileOutputStream(file);

// 執(zhí)行下載

gridFSBucket.downloadToStream(filename, os);

System.out.println("下載成功");

return "ok";

}

/**

* 刪除文件

* @param model

* @return

* @throws Exception

*/

@GetMapping("/deleteGirdFS")

@ResponseBody

public String deleteGirdFS(Model model) throws Exception {

// 獲取文件ID

String objectId = "5e42924980fb940ab75f141a";

// 執(zhí)行刪除

gridFSBucket.delete(new ObjectId(objectId));

return "ok";

}

5. 整合SpringBoot作為文件服務(wù)展示

當(dāng)然也能和nginx放在一起使用

/**

* 借助nginx+springboot來訪問gridfs中的文件內(nèi)容

* @param fileName

* @return

* @throws Exception

*/

@GetMapping("/detail/{fileName}")

public void detail(@PathVariable("fileName") String fileName, HttpServletResponse response) throws Exception {

response.setContentType("text/html;charset=utf-8");

ServletOutputStream outputStream = null;

try {

outputStream = response.getOutputStream();

gridFSBucket.downloadToStream(fileName, outputStream);

// 方式2

// GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(fileName);

// GridFsResource gridFsResource = new GridFsResource(gridFSDownloadStream.getGridFSFile(), gridFSDownloadStream);

// String data = IOUtils.toString(gridFsResource.getInputStream(), "UTF-8");

// System.out.println(data);

// out.write(data.getBytes("UTF-8"));

// out.write("風(fēng)間影月".getBytes("UTF-8"));

} finally {

outputStream.close();

}

}

總結(jié)

以上是生活随笔為你收集整理的java mongodb gridfs_MongoDB-4 GridFS 文件存储的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。