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

1.引入oss依赖


    
    
        com.aliyun.oss
        aliyun-sdk-oss
    
    
    
        joda-time
        joda-time
    
    

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

#服务端口
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;
        }
    }
}

你可能感兴趣的:(java,数据库,大数据)