mongoDB 文件操作


mongoDB 文件操作

 

 

****************************

相关类与接口

 

GridFsTemplate

public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver {


************
构造方法

    public GridFsTemplate(MongoDbFactory dbFactory, MongoConverter converter) {
    public GridFsTemplate(MongoDbFactory dbFactory, MongoConverter converter, @Nullable String bucket) {


************
常用方法

    public ObjectId store(InputStream content, String filename) {
    public ObjectId store(InputStream content, @Nullable Object metadata) {
    public ObjectId store(InputStream content, @Nullable Document metadata) {
    public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType) {
    public ObjectId store(InputStream content, @Nullable String filename, @Nullable Object metadata) {
    public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType, @Nullable Object metadata) {
    public ObjectId store(InputStream content, @Nullable String filename, @Nullable Document metadata) {
    public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType, @Nullable Document metadata) {

    public GridFSFindIterable find(Query query) {
    public GridFSFile findOne(Query query) {

    public void delete(Query query) {
    public GridFsResource getResource(GridFSFile file) {

 

GridFsResource

public class GridFsResource extends InputStreamResource {

    static final String CONTENT_TYPE_FIELD = "_contentType";
    private static final ByteArrayInputStream EMPTY_INPUT_STREAM = new ByteArrayInputStream(new byte[0]);
    @Nullable
    private final GridFSFile file;
    private final String filename;


*********
构造方法

    private GridFsResource(String filename)
    public GridFsResource(GridFSFile file)
    public GridFsResource(GridFSFile file, InputStream inputStream)


*********
常用方法

    public static GridFsResource absent(String filename) 
    public InputStream getInputStream() throws IOException, IllegalStateException {
    public long contentLength() throws IOException {
    public String getFilename() throws IllegalStateException {

    public boolean exists() {
    public long lastModified() throws IOException {
    public String getContentType() {

 

GridFsBuckets

public final class GridFSBuckets {
    public static GridFSBucket create(MongoDatabase database) {
        return new GridFSBucketImpl(database);
    }

    public static GridFSBucket create(MongoDatabase database, String bucketName) {
        return new GridFSBucketImpl(database, bucketName);
    }

    private GridFSBuckets() {
    }
}

 

GridFsBucket:下载接口

@ThreadSafe
public interface GridFSBucket {

    GridFSDownloadStream openDownloadStream(ObjectId var1);

    。。。
}

 

 

****************************

使用示例

 

***************

controller 层

 

HelloController

@RestController
public class HelloController {

    @Autowired
    private GridFsTemplate gridFsTemplate;

    @Autowired
    private MongoDbFactory mongoDbFactory;

    private Map<String,String> map=new HashMap<>();

    @RequestMapping("/upload")
    public String upload(MultipartFile file){
        ObjectId objectId=null;
        System.out.println(file.getContentType());
        System.out.println(file.getName());
        System.out.println(file.getOriginalFilename());

        try{
            objectId=gridFsTemplate.store(file.getInputStream(),file.getOriginalFilename(),file.getContentType());
            map.put("fileId",objectId.toString());
        }catch (Exception e){
            e.printStackTrace();
        }

        assert objectId != null;
        return objectId.toString();
    }

    @RequestMapping("/get")
    public void get(HttpServletResponse response){
        Query query=Query.query(Criteria.where("_id").is(map.get("fileId")));
        GridFSFile gridFSFile=gridFsTemplate.findOne(query);

        assert gridFSFile != null;
        GridFsResource gridFsResource=new GridFsResource(gridFSFile,
                GridFSBuckets.create(mongoDbFactory.getDb()).openDownloadStream(gridFSFile.getObjectId()));

        response.reset();
        try{
            response.setContentType("application/octet-stream");
            response.setContentLength((int)gridFSFile.getLength());
            response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(Objects.requireNonNull(gridFsResource.getFilename()),"utf-8"));
            IOUtils.copy(gridFsResource.getInputStream(),response.getOutputStream());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @RequestMapping("/delete")
    public String delete(){
        Query query=Query.query(Criteria.where("_id").is(map.get("fileId")));
        gridFsTemplate.delete(query);

        return "success";
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值