• oss存储


    <!--        oss存储-->
            <dependency>
                <groupId>io.minio</groupId>
                <artifactId>minio</artifactId>
                <version>3.0.10</version>
            </dependency>
    

      

    package com.yixintech.cloud.dossmodel.rest;
    
    import com.yixintech.cloud.auth.client.annotation.IgnoreClientToken;
    import com.yixintech.cloud.auth.client.annotation.IgnoreUserToken;
    import io.minio.MinioClient;
    import org.apache.commons.io.IOUtils;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.servlet.http.HttpServletResponse;
    import java.io.InputStream;
    @RestController
    @IgnoreUserToken
    @IgnoreClientToken
    @RequestMapping("oss")
    public class MinioController {
       private static String url = "http://127.0.0.1:9000";  //minio服务的IP端口
       private static String accessKey = "minioadmin";
       private static String secretKey = "minioadmin";
       
        //上传文件到minio服务
     @PostMapping("upload")
     public String upload(@RequestParam("fileName") MultipartFile file )  {
       try {
           MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
           InputStream is= file.getInputStream(); //得到文件流
           String fileName = file.getOriginalFilename(); //文件名
           String contentType = file.getContentType();  //类型
           minioClient.putObject("duan",fileName,is,contentType); //把文件放置Minio桶(文件夹)
           return  "上传成功";
         }catch (Exception e){
             return "上传失败";
         }
        }
        //下载minio服务的文件
        @GetMapping("download")
        public String download(HttpServletResponse response){
            try {
              MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
              InputStream fileInputStream = minioClient.getObject("file", "test.jpg");
              response.setHeader("Content-Disposition", "attachment;filename=" + "test.jpg");
              response.setContentType("application/force-download");
              response.setCharacterEncoding("UTF-8");
              IOUtils.copy(fileInputStream,response.getOutputStream());
              return "下载完成";
            }catch (Exception e){
                return "下载失败";
            }
        }
        //获取minio文件的下载地址
        @GetMapping("url")
        public  String  getUrl(){
            try {
                MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
                String url = minioClient.presignedGetObject("duan", "test.jpg");
                return url;
            }catch (Exception e){
                return "获取失败";
            }
        }
    }
    

      

  • 相关阅读:
    传统神经网络所存在的问题
    Xcode 全局搜索中文的正则表达式
    ES2020可选链"?."
    ng : 无法将“ng”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
    阿里云盾反爬虫(Anti-Bot)产品方案浅析
    使用spring连接mysql数据库出错
    Win10 cmd的ssh命令连接linux虚拟机
    python不换行输出
    计算机网络-CSMA/CD
    计算机网络-奈氏准则
  • 原文地址:https://www.cnblogs.com/gfbzs/p/13553091.html
Copyright © 2020-2023  润新知