谷粒-oss的使用-上传文件

1.引入oss依赖

<dependencies>
    <!-- 阿里云oss依赖 -->
    <dependency>
        <groupId>com.aliyun.oss</groupId>
        <artifactId>aliyun-sdk-oss</artifactId>
    </dependency>
    <!-- 日期工具栏依赖 -->
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
    </dependency>
    </dependencies>

在配置文件中进行三个相关的配置

#服务端口
server.port=8002
#服务名
spring.application.name=service-oss
#环境设置:dev、test、prod
spring.profiles.active=dev


#阿里云 OSS
#不同的服务器,地址不同
aliyun.oss.file.endpoint=oss-cn-beijing.aliyuncs.com
aliyun.oss.file.keyid=LTAI5tJcNMHvSN3MxsoQ5MqT
aliyun.oss.file.keysecret=Bt2WemX4WvsqgNqbOTBpivHedpsxSz
#bucket可以在控制台创建,也可以使用java代码创建
aliyun.oss.file.bucketname=guli-liao

配置启动类,并排除数据库

package cn.lf.oss;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ComponentScan({"cn.lf"})
public class App_Oss {
    public static void main(String[] args) {
        SpringApplication.run(App_Oss.class, args);
    }
}

//上传文件后,目的得到上传文件的url地址

1.编写两个类controller和service

2.controller和前端交互得到数据,然后调用service里面的方法进行操作

package cn.lf.oss.controller;


import cn.lf.common_utils.R;
import cn.lf.oss.service.Oss_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;


@Controller
@RequestMapping("eduoss/upfile")
public class oss_controller {
    @Autowired
    private Oss_Service oss_service;

    @PostMapping
    public R uploadFile(MultipartFile file){
        //获取到上传的文件
        //将路径进行返回
        String url = oss_service.uploadFile(file);
        return R.ok().data("url",url);
    }
}

 service层代码

package cn.lf.oss.service.Impl;

import cn.lf.oss.service.Oss_Service;
import cn.lf.oss.utils.oss_utils;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Service
public class Oss_ServiceImpl implements Oss_Service {
    @Override
    public String uploadFile(MultipartFile file) {
        //这里的代码参照oss文档
        // 编写一个类来获取相关的值,从配置文件中获取
        String endpoint = oss_utils.END_POINT;
        // 阿里云账号AccessKey拥
        String accessKeyId = oss_utils.ACCESS_KEY_ID;
        String accessKeySecret = oss_utils.ACCESS_KEY_SECRET;
        // 填写Bucket名称,
        String bucketName = oss_utils.BUCKET_NAME;

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            //获取上传你文件的输入流
            InputStream inputStream = file.getInputStream();
            //获取上传你文件的名字
            String filename = file.getOriginalFilename();
            //在文件里面添加唯一的值
            String uuid = UUID.randomUUID().toString().replace("-", "");
            filename = uuid+filename;
            //按照日期进行分类
            String dataPath = new DateTime().toString("yyyy/MM/dd");
            filename = dataPath + "/" + filename;
            ossClient.putObject(bucketName, filename, inputStream);
            ossClient.shutdown();
            //上传文件之后的路径返回
            //需要把路径拼接出来
            //https://guli-liao.oss-cn-beijing.aliyuncs.com/01.jpg
            String url = "https://" + bucketName + "." + endpoint + "/" + filename;
            return url;

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值