一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 使用Springboot整合GridFS實(shí)現(xiàn)文件操作

使用Springboot整合GridFS實(shí)現(xiàn)文件操作

2022-02-26 00:28積木i Java教程

這篇文章主要介紹了使用Springboot整合GridFS實(shí)現(xiàn)文件操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

GridFsOperations,實(shí)現(xiàn)GridFS文件上傳下載刪除

最近學(xué)習(xí)GridFS,想用它整合springboot弄個(gè)文件的上傳下載。

網(wǎng)上查到的很多資料都是使用GridFsTemplate,還有GridFSBucket來實(shí)現(xiàn)的,需要各種額外配置Bean。但是看了spring-data-mongodb的官方文檔,以及示例代碼,他們只用到了GridFsOperations,無需其他任何配置。

然后就用GridFsOperations寫了個(gè)文件上傳下載的demo,用起來還是很方便的,給大家個(gè)參考。

上傳下載刪除功能實(shí)現(xiàn)

pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

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<String , ObjectId> upload(@Parameter @RequestPart(value = "file") MultipartFile file){
      //開始時(shí)間
      long begin = System.nanoTime();
      Map<String,ObjectId> map = new HashMap<>();
      try{
          InputStream streamForUpload = file.getInputStream();
          ObjectId objectId = gridFsOperations.store(streamForUpload,file.getOriginalFilename(),file.getContentType());
          //上傳結(jié)束
          long end = System.nanoTime();
          long time = end-begin;
          System.out.println("本次上傳共耗時(shí): "+ 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 filename, HttpServletResponse response) throws IOException {
      //開始時(shí)間
      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);
      //注意: 如果沒有下面這行設(shè)置header, 結(jié)果會將文件的內(nèi)容作為響應(yīng)的 body 直接輸出在頁面上, 而不是下載文件
      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();
      //下載結(jié)束
      long end = System.nanoTime();
      long time = end-begin;
      System.out.println("本次下載共耗時(shí): "+ time);
      return contentType;
  }
  @DeleteMapping("/delete")
  public String deleteFile(@Parameter @RequestParam("filename") String filename){
      gridFsOperations.delete(query(whereFilename().is(filename)));
      return "delete success";
  }
}

 

測試

上傳

使用Springboot整合GridFS實(shí)現(xiàn)文件操作 使用Springboot整合GridFS實(shí)現(xiàn)文件操作

下載

紅色圈內(nèi)點(diǎn)擊download就可以下載啦?;蛘咴诘刂窓谥苯虞斎雔ocalhost:8080/file/download?filename=todo.txt 也可以直接下載文件(這里的todo.txt是我測試的文件,你們填自己上傳的文件名,不要忘了加上后綴名?。?/p>

使用Springboot整合GridFS實(shí)現(xiàn)文件操作

刪除

使用Springboot整合GridFS實(shí)現(xiàn)文件操作

上面這些上傳刪除功能測試的時(shí)候,大家也可以結(jié)合mongodb的數(shù)據(jù)庫去看看。

使用Springboot整合GridFS實(shí)現(xiàn)文件操作

上傳的文件類型不限,大小嘛,看你properties文件里設(shè)置的上限是多大了。我拿700MB的文件上傳了也ok,然后在數(shù)據(jù)庫中會被分成很多個(gè)塊進(jìn)行存儲。具體存儲的細(xì)節(jié)和原理,網(wǎng)上文檔很多,這兒就不嘮叨嘞。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/Lao_gan_ma/article/details/117295052

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: t66y地址一地址二地址三 | 成人国产精品视频频 | 亚洲精品一区二区三区中文字幕 | 免费国产白棉袜踩踏区域 | 美国高清xxxxx18| 91精品乱码一区二区三区 | 亚洲2017天堂色无码 | 美艳教师刘艳第三部166 | 免看一级a一片成人123 | 啪哆哆 | 国产91影院 | 免费看欧美一级特黄a大片一 | 99久久久久国产 | 男人猛进猛出女人下面视频 | 欧美特黄视频在线观看 | 亚洲激情在线 | 好大好硬好深好爽想要吃奶 | 美女脱了内裤让男桶爽 | 久久精品动漫99精品动漫 | 国产全部视频 | 好男人资源免费播放 | 国产亚洲精品高清在线 | 国产成人毛片 | 果冻传媒在线免费观看 | 我把寡妇日出水好爽 | 九九热免费在线观看 | 成人在线视频在线观看 | 草久热 | 免费黄色片在线观看 | 香蕉视频在线观看网址 | 7777奇米四色 | 网友自拍咪咪爱 | 久久国产36精品色熟妇 | 四虎影院4hu | 欧美一区二区三区高清不卡tv | 青春草在线观看精品免费视频 | 四虎影院的网址 | 丝瓜草莓香蕉绿巨人幸福宝 | 9191久久 | 亚洲第一综合天堂另类专 | 国产精品怡红院永久免费 |