使用Springboot整合GridFS实现文件操作

网友投稿 636 2022-11-29

使用Springboot整合GridFS实现文件操作

使用Springboot整合GridFS实现文件操作

目录GridFsOperations,实现GridFS文件上传-删除上传-删除功能实现测试上传-删除

GridFsOperations,实现GridFS文件上传-删除

最近学习GridFS,想用它整合springboot弄个文件的上传-。

网上查到的很多资料都是使用GridFsTemplate,还有GridFSBucket来实现的,需要各种额外配置Bean。但是看了spring-data-mongodb的官方文档,以及示例代码,他们只用到了GridFsOperations,无需其他任何配置。

然后就用GridFsOperations写了个文件上传-的demo,用起来还是很方便的,给大家个参考。

上传-删除功能实现

pom.xml

org.springframework.boot

spring-boot-starter-data-mongodb

application.properties

#文件上传-配置

spring.servlet.multipart.max-file-size=1024MB

spring.servlet.multipart.max-request-size=1024MB

FileController

package com.example.tryRedis.controller;

import static org.springframework.data.mongodb.core.query.Query.*;

import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;

import com.mongodb.client.gridfs.model.GridFSFile;

import io.swagger.v3.oas.annotations.Parameter;

import org.bson.types.ObjectId;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.mongodb.gridfs.GridFsOperations;

import org.springframework.data.mongodb.gridfs.GridFsResource;

import org.springframework.http.MediaType;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletResponse;

import java.io.*;

import java.util.HashMap;

import java.util.Map;

@RestController

@RequestMapping("/file")

public class FileController {

@Autowired

GridFsOperations gridFsOperations;

//上传文件

@PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

public Map upload(@Parameter @RequestPart(value = "file") MultipartFile file){

//开始时间

long begin = System.nanoTime();

Map map = new HashMap<>();

try{

InputStream streamForUpload = file.getInputStream();

ObjectId objectId = gridFsOperations.store(streamForUpload,file.getOriginalFilename(),file.getContentType());

//上传结束

long end = System.nanoTime();

long time = end-begin;

System.out.println("本次上传共耗时: "+ time);

System.out.println("上传成功!文件名: "+file.getOriginalFilename()+". 文件ID: "+objectId);

map.put(file.getOriginalFilename(),objectId);

}catch (Exception e){

e.printStackTrace();

}

return map;

}

//查询并-文件

@GetMapping("/download")

public String download(String filenWGSxjbRame, HttpServletResponse response) throws IOException {

//开始时间

long begin = System.nanoTime();

//查询文件

GridFSFile result = gridFsOperations.findOne(query(whereFilename().is(filename)));

GridFsResource gridFsResource= gridFsOperations.getResource(result);

String contentType = gridFsResource.getContentType();

System.out.println("contentType: "+contentType);

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

response.reset();

response.setContentType(contentType);

//注意: 如果没有下面这行设置header, 结果会将文件的内容作为响应的 body 直接输出在页面上, 而不是-文件

http:// response.setHeader("Content-Disposition","attachment;filename="+filename); //指定-文件名

ServletOutputStream outputStream = response.getOutputStream();

InputStream is = gridFsResource.getInputStream();

byte[] bytes = new byte[1024];

int len = 0;

while ((len=is.read(bytes))!=-1){

outputStream.write(bytes,0,len);

}

is.close();

outputStream.close();

//-结束

long end = System.nanoTime();

long time = end-begin;

System.out.println("本次-共耗时: "+ time);

return contentType;

}

@DeleteMapping("/delete")

public String deleteFile(@Parameter @RequestParam("filename") String filename){

gridFsOperations.delete(query(whereFilename().is(filename)));

return "delete success";

}

}

测试

上传

-

红色圈内点击download就可以-啦。或者在地址栏直接输入localhost:8080/file/download?filename=todo.txt 也可以直接-文件(这里的todo.txt是我测试的文件,你们填自己上传的文件名,不要忘了加上后缀名!)

删除

上面这些上传删除功能测试的时候,大家也可以结合mongodb的数据库去看看。

上传的文件类型不限,大小嘛,看你properties文件里设置的上限是多大了。我拿700MB的文件上传了也ok,然后在数据库中会被分成很多个块进行存储。具体存储的细节和原理,网上文档很多,这儿就不唠叨嘞。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:在天地图中添加多边形及注册面的点击事件
下一篇:部署ssm项目到云服务器遇到的坑
相关文章

 发表评论

暂时没有评论,来抢沙发吧~