4zhvml8 发表于 2024-10-26 21:01:49

SpringBoot整合七牛云实现文件上传


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在做项目的时候,<span style="color: black;">咱们</span>经常会遇到文件上传的<span style="color: black;">需要</span>,<span style="color: black;">例如</span>用户更换头像、上传详情<span style="color: black;">照片</span>等等,这个时候<span style="color: black;">能够</span>将上传的文件存放到项目的根目录下面,<span style="color: black;">然则</span>随着文件的数量的<span style="color: black;">增加</span>,文件会<span style="color: black;">欠好</span>管理并且<span style="color: black;">拜访</span>的速度<span style="color: black;">亦</span>不是<span style="color: black;">火速</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因此</span><span style="color: black;">能够</span><span style="color: black;">思虑</span>存储空间进行文件的存储。对象存储是面向对象/文件的、海量的互联网存储,它<span style="color: black;">亦</span><span style="color: black;">能够</span>直接被<span style="color: black;">叫作</span>为“云存储”。对象尽管是文件,它是已被封装的文件(编程中的对象就有封装性的特点),<span style="color: black;">亦</span><span style="color: black;">便是</span>说,在对象存储系统里,你<span style="color: black;">不可</span>直接打开/修改文件,但<span style="color: black;">能够</span>像ftp<span style="color: black;">同样</span>上传文件,下载文件等</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">另一</span>对象存储<span style="color: black;">无</span>像文件系统那样有一个<span style="color: black;">非常多</span>层级的文件结构,而是<span style="color: black;">仅有</span>一个</span><span style="color: black;"><span style="color: black;">“桶”(bucket)</span></span><span style="color: black;">的概念(<span style="color: black;">亦</span><span style="color: black;">便是</span>存储空间),“桶”里面<span style="color: black;">所有</span>都是对象,是一种非常扁平化的存储方式。其最大的特点<span style="color: black;">便是</span>它的对象名<span style="color: black;">叫作</span><span style="color: black;">便是</span>一个域名<span style="color: black;">位置</span>,一旦对象被设置为“公开”,所有网民都<span style="color: black;">能够</span><span style="color: black;">拜访</span>到它;它的<span style="color: black;">持有</span>者还<span style="color: black;">能够</span><span style="color: black;">经过</span>REST API的方式<span style="color: black;">拜访</span>其中的对象</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因此呢</span>,对象存储最主流的<span style="color: black;">运用</span>场景,<span style="color: black;">便是</span>存储网站、移动app等互联网/移动互联网应用的静态内容(视频、<span style="color: black;">照片</span>、文件、软件安装包等等)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">由于</span>七牛云的对象存储空间是免费的,并且容量<span style="color: black;">亦</span>是相对够用的(<span style="color: black;">因此</span>还是<span style="color: black;">选取</span>白嫖一波吧)</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">准备工作</h1><span style="color: black;">注册七牛云账号,<span style="color: black;">而后</span>到<span style="color: black;">掌控</span>台中<span style="color: black;">选取</span>存储空间</span>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/5d752ae2dcf643c5a21df1686df74b0d~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729479280&amp;x-signature=0%2B0XMywxcsuilAh41stn1CJuUNs%3D" style="width: 50%; margin-bottom: 20px;"></div><span style="color: black;">进去之后点击新建空间,一个空间相当于是一个文件夹,存放对象的位置</span>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/b935f75ec22648f3b324befe115d3ec1~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729479280&amp;x-signature=4rPrN16SZmZDa%2BKZCjRyFWBPLTQ%3D" style="width: 50%; margin-bottom: 20px;"></div><span style="color: black;">创建完成之后<span style="color: black;">能够</span><span style="color: black;">按照</span>自己的需要<span style="color: black;">是不是</span>绑定域名</span><span style="color: black;"><span style="color: black;">(绑定的域名必须是备案过的)</span></span>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/82d83b88b9bf414b9c9b5a58d4a8b99d~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729479280&amp;x-signature=sn3z4yzhKqOSQNCiQymrnuYJyyw%3D" style="width: 50%; margin-bottom: 20px;"></div><span style="color: black;">点击密钥管理查看自己的密钥,在之后的文件上传和下载中都会用到</span>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/55a0ea1a12a3413482a3c908d3312a99~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729479280&amp;x-signature=rdISjdB8XL%2FjLaGUW7FbBhklNTQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">代码测试</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1、在pom.xml中引入对应的依赖</h1><span style="color: black;">&lt;!-- 七牛云对应依赖 --&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">dependency</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">groupId</span>&gt;</span>com.qiniu<span style="color: black;">&lt;/<span style="color: black;">groupId</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">artifactId</span>&gt;</span>qiniu-java-sdk<span style="color: black;">&lt;/<span style="color: black;">artifactId</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">version</span>&gt;</span><span style="color: black;">&lt;/<span style="color: black;">version</span>&gt;</span>
    <span style="color: black;">&lt;/<span style="color: black;">dependency</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">dependency</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">groupId</span>&gt;</span>com.google.code.gson<span style="color: black;">&lt;/<span style="color: black;">groupId</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">artifactId</span>&gt;</span>gson<span style="color: black;">&lt;/<span style="color: black;">artifactId</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">version</span>&gt;</span>2.8.5<span style="color: black;">&lt;/<span style="color: black;">version</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">scope</span>&gt;</span>compile<span style="color: black;">&lt;/<span style="color: black;">scope</span>&gt;</span>
    <span style="color: black;">&lt;/<span style="color: black;">dependency</span>&gt;</span>
    <span style="color: black;">&lt;!-- lombok依赖 --&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">dependency</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">groupId</span>&gt;</span>org.projectlombok<span style="color: black;">&lt;/<span style="color: black;">groupId</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">artifactId</span>&gt;</span>lombok<span style="color: black;">&lt;/<span style="color: black;">artifactId</span>&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">optional</span>&gt;</span>true<span style="color: black;">&lt;/<span style="color: black;">optional</span>&gt;</span>
    <span style="color: black;">&lt;/<span style="color: black;">dependency</span>&gt;</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">2、编写配置application.yml配置文件</h1><span style="color: black;">qiniuyun:</span>
    <span style="color: black;">#你的accessKey密钥</span>
    <span style="color: black;">accessKey:</span> <span style="color: black;">xxx</span>
    <span style="color: black;">#你的secretKey密钥</span>
    <span style="color: black;">secretKey:</span> <span style="color: black;">xxx</span>
    <span style="color: black;">#存储空间名<span style="color: black;">叫作</span></span>
    <span style="color: black;">bucket:</span> <span style="color: black;">xxx</span>
    <span style="color: black;">#外链域名,即空间的默认域名</span>
    <span style="color: black;">qiniu-image-domain:</span> <span style="color: black;">xxx</span>
    <span style="color: black;">#链接过期时间,单位为秒,-1<span style="color: black;">暗示</span><span style="color: black;">不外</span>期</span>
    <span style="color: black;">expire-in-seconds:</span> <span style="color: black;">-1</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">3、编写QiNiuUtils工具类</h1><span style="color: black;">@Data</span>
    <span style="color: black;">@Component</span>
    <span style="color: black;">@ConfigurationProperties</span>(prefix = <span style="color: black;">"qiniuyun"</span>)
    <span style="color: black;">public</span> <span style="color: black;">class</span> QiNiuUtils {

    <span style="color: black;">private</span> <span style="color: black;">String</span> accessKey;

    <span style="color: black;">private</span> <span style="color: black;">String</span> secretKey;

    <span style="color: black;">private</span> <span style="color: black;">String</span> bucket;

    <span style="color: black;">@Value</span>(<span style="color: black;">"${qiniuyun.qiniu-image-domain}"</span>)
    <span style="color: black;">private</span> <span style="color: black;">String</span> QINIU_IMAGE_DOMAIN;

    <span style="color: black;">@Value</span>(<span style="color: black;">"${qiniuyun.expire-in-seconds}"</span>)
    <span style="color: black;">private</span> long expireSeconds;

    <span style="color: black;">public</span> Auth getAuth(){
    <span style="color: black;">return</span> Auth.create(accessKey,secretKey);
    }

    <span style="color: black;">/*
      上传文件到存储空间
      */</span>
    <span style="color: black;">public</span> <span style="color: black;">String</span>upload(MultipartFile file){<span style="color: black;">//构造一个带指定 Region 对象的配置类</span>
    Configuration cfg = <span style="color: black;">new</span> Configuration(Region.huadongZheJiang2());
    <span style="color: black;">//...其他参数参考类注释</span>UploadManager uploadManager =<span style="color: black;">new</span> UploadManager(cfg);
    Auth auth = getAuth();
    <span style="color: black;">String</span> upToken = auth.uploadToken(bucket);
    <span style="color: black;">String</span> url = <span style="color: black;">null</span>;
    <span style="color: black;">try</span>{
    Response response = uploadManager.put(file.getInputStream(), file.getOriginalFilename(), upToken,<span style="color: black;">null</span>,<span style="color: black;">null</span>);
    url = QINIU_IMAGE_DOMAIN + JSONObject.parseObject(response.bodyString()).get(<span style="color: black;">"key"</span>);
    <span style="color: black;">//解析上传成功的结果</span>
    DefaultPutRet putRet = <span style="color: black;">new</span>Gson().fromJson(response.bodyString(), DefaultPutRet.class);
    System.out.println(putRet.key);
    System.out.println(putRet.hash);<span style="color: black;">//将上传完的<span style="color: black;">照片</span>的外链返回出去</span>
    <span style="color: black;">return</span> url;
    } <span style="color: black;">catch</span>(Exception ex) {
    ex.printStackTrace();
    System.err.println(ex.toString());
    }<span style="color: black;">return</span> url;
    }

    <span style="color: black;">/*
      查看七牛云存储空间中的所有文件
      */</span>
    <span style="color: black;">public</span> <span style="color: black;">void</span> cat(){
    <span style="color: black;">//构造一个带指定 Region 对象的配置类</span>
    Configuration cfg = <span style="color: black;">new</span>Configuration(Region.region0());
    Auth auth = getAuth();
    BucketManager manager =<span style="color: black;">new</span> BucketManager(auth, cfg);
    <span style="color: black;">//要查看文件名的前缀</span>
    <span style="color: black;">String</span> prefix = <span style="color: black;">"Bicon"</span>;
    int limit = <span style="color: black;">1000</span>;
    <span style="color: black;">String</span>delimiter =<span style="color: black;">""</span>;
    BucketManager.FileListIterator fileListIterator = manager.createFileListIterator(bucket, prefix, limit, delimiter);
    <span style="color: black;">while</span>(fileListIterator.hasNext()) {<span style="color: black;">//处理获取的file list结果</span>
    FileInfo[] items = fileListIterator.next();
    List&lt;FileInfo&gt; fileInfos = Arrays.asList(items);
    List&lt;<span style="color: black;">String</span>&gt; collect = fileInfos.stream().map(fileInfo -&gt; {<span style="color: black;">return</span> QINIU_IMAGE_DOMAIN+fileInfo.key;
    }).collect(Collectors.toList());
    <span style="color: black;">//将所有符合前缀的文件进行<span style="color: black;">表示</span></span>System.out.println(collect);
    }
    }<span style="color: black;">/*
      获取文件<span style="color: black;">拜访</span><span style="color: black;">位置</span>
      */</span>
    <span style="color: black;">public</span> <span style="color: black;">String</span> fileUrl(<span style="color: black;">String</span> fileName) throws UnsupportedEncodingException {
    <span style="color: black;">String</span>encodedFileName = URLEncoder.encode(fileName,<span style="color: black;">"utf-8"</span>);
    <span style="color: black;">String</span> publicUrl = <span style="color: black;">String</span>.format(<span style="color: black;">"%s/%s"</span>, QINIU_IMAGE_DOMAIN, encodedFileName);
    Auth auth = getAuth();
    long expireInSeconds = expireSeconds;<span style="color: black;">if</span>(<span style="color: black;">-1</span> == expireInSeconds){
    <span style="color: black;">return</span> auth.privateDownloadUrl(publicUrl);
    }
    <span style="color: black;">return</span>auth.privateDownloadUrl(publicUrl, expireInSeconds);
    }

    }<h1 style="color: black; text-align: left; margin-bottom: 10px;">4、编写UploadController</h1><span style="color: black;">@RestController</span>
    <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">UploadController</span> </span>{

    <span style="color: black;">//引入<span style="color: black;">刚才</span>写好的工具类</span>
    <span style="color: black;">@Autowired</span>
    <span style="color: black;">private</span> QiNiuUtils qiNiuUtils;

    <span style="color: black;">@RequestMapping(<span style="color: black;">"/upload"</span>)</span>
    <span style="color: black;">@CrossOrigin</span>
    <span style="color: black;">public</span> String upload(<span style="color: black;">@RequestParam(<span style="color: black;">"file"</span>)</span> MultipartFile file) throws IOException {
    <span style="color: black;">//获取到上传<span style="color: black;">照片</span>的外链</span>String upload = qiNiuUtils.upload(file);<span style="color: black;">return</span> upload;
    }

    }<h1 style="color: black; text-align: left; margin-bottom: 10px;">5、在vue页面中添加一个上传组件</h1><span style="color: black;">&lt;<span style="color: black;">template</span>&gt;</span>
    <span style="color: black;">&lt;!--
      on-success:上传成功后调用的<span style="color: black;">办法</span>
      action:选中文件之后的请求路径
      auto-upload:是否自动上传文件
      --&gt;</span>
    <span style="color: black;">&lt;<span style="color: black;">el-upload</span> <span style="color: black;">:on-success</span>=<span style="color: black;">"upload"</span> <span style="color: black;">:action</span>=<span style="color: black;">"action"</span> <span style="color: black;">list-type</span>=<span style="color: black;">"picture-card"</span> <span style="color: black;">:auto-upload</span>=<span style="color: black;">"true"</span>&gt;</span>
    <span style="color: black;">&lt;/<span style="color: black;">template</span>&gt;</span>

    <span style="color: black;">&lt;<span style="color: black;">script</span>&gt;</span><span style="color: black;">

      <span style="color: black;">export</span> <span style="color: black;">default</span>{
      data(){<span style="color: black;">return</span>{
      <span style="color: black;">action</span>: <span style="color: black;">http://localhost:8080/upload</span>
      }
      }
      <span style="color: black;">methods</span>:{
      upload(response){
      <span style="color: black;">if</span>(response.code==<span style="color: black;">200</span>){
      <span style="color: black;">this</span>.formAdd.gimg = response.obj<span style="color: black;">this</span>.$message.success(<span style="color: black;">上传成功!</span>)
      }<span style="color: black;">else</span>{
      <span style="color: black;">this</span>.$message.error(<span style="color: black;">上传<span style="color: black;">照片</span>失败!</span>)
      }
      },
      }
      }
    </span><span style="color: black;">&lt;/<span style="color: black;">script</span>&gt;</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">6、在前端页面中<span style="color: black;">选取</span>文件并查看效果,<span style="color: black;">或</span><span style="color: black;">能够</span>用API测试工具来进行测试</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>我<span style="color: black;">运用</span>的是Apifox,<span style="color: black;">大众</span><span style="color: black;">亦</span><span style="color: black;">能够</span>用其他的API调试工具</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/1fddb4e6852e43958253784bc78e11df~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729479280&amp;x-signature=HjM4NEuKRmobGxKSBIGaiaG2hRo%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">7、查看效果</h1>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/a9dfc670bc944ca3932accc6a68ac3b8~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729479280&amp;x-signature=93rzCvxu0tJ4NVRV27YJE5mNGmc%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">到<span style="color: black;">这儿</span>,<span style="color: black;">已然</span>实现了简单的将文件上传到七牛云上面啦,<span style="color: black;">这儿</span>的文件上传组件是选中完文件自动上传了,<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">按照</span>自己的需要变成点击按钮进行上传</span></p>




页: [1]
查看完整版本: SpringBoot整合七牛云实现文件上传