java oss 批量传输_阿里云OSS存储、头像上传 - 龙欺

本文档详细介绍了如何使用阿里云OSS服务进行头像上传和存储,包括后端接口编写、Service层实现、配置文件设置以及Swagger测试。通过Spring Boot和Vue.js实现前后端交互,确保文件安全存储并能成功访问。
摘要由CSDN通过智能技术生成

相信大家在做web开发过程中肯定会遇到头像上传功能,这里使用OSS进行头像上传存储

先登录:

1e66d4f747c12b3b60ebbb541838d743.png

 这里最好选择支付宝扫码登录,相信大家都进行了支付宝的实名认证

如果可以的话,可以往账户里存1块钱,这样不会造成存储量过大欠费

然后选择产品中的存储,OSS

d0b581135c22a7530b8e455f41d97afe.png

 进来后选择开通,因为我实现已经开通好了,所以我这里是管理控制台

e393198e35de1d54c0b2f58970bbbfcd.png

 下面进入后台接口编写

首先,创建一个对外访问接口

import com.atguigu.commonutils.Result;
import com.atguigu.oss.service.impl.FileServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(description = "阿里云文件管理")
@RestController
@RequestMapping("/oss/file")
@CrossOrigin
publicclass FileController {

    @Autowired
    private FileServiceImpl fileService;

    @ApiOperation(value = "文件上传")
    @PostMapping("/upload")
    public Result upload(@ApiParam(name = "file",value = "文件",required = true) @RequestParam("file") MultipartFile multipartFile){
        String url = (multipartFile);
        return Result.succ().message("文件上传成功").data("url",url);
    }

}

编写service层接口

import org.springframework.web.multipart.MultipartFile;

publicinterface FileService {

    /**
     * 文件上传至阿里云
     * @param file
     * @return*/
    String upload(MultipartFile file);

}

实现接口

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.atguigu.oss.service.FileService;
import com.atguigu.oss.util.ConstantPropertiesUtil;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

@Service
publicclass FileServiceImpl implements FileService {

    /**
     * 文件上传至阿里云
     *
     * @param file
     * @return*/
    @Override
    public String upload(MultipartFile file) {

        String endPoint = ConstantPropertiesUtil.END_POINT;
        String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
        String bucketName = ConstantPropertiesUtil.BUCKET_NAME;

        String uploadUrl = null;

        try {
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
            if (!(bucketName)){
                //创建bucket                ossClient.createBucket(bucketName);
                //设置oss实例的访问权限:公共读                (bucketName, );
            }
            // 获取上传文件流。
            InputStream inputStream = ();

            String filename = ();

            String uuid = ().toString().replaceAll("-","");
            filename = uuid+filename;

            String datePath = new DateTime().toString("yyyy/MM/dd");

            filename = datePath+"/"+filename;

            //文件上传至阿里云            (bucketName, filename, inputStream);
            //关闭OSSClient            ossClient.shutdown();

            uploadUrl = "https://"+bucketName+"."+endPoint+"/"+filename;

        }catch (Exception e){
            ();
        }

        return uploadUrl;
    }

}

这里因为需要用到几个固定值

所以我写在了配置文件中

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

#阿里云 OSS
#不同的服务器,地址不同
=your
=your
=your

#bucket可以在控制台创建,也可以使用java代码创建
=your

下面写一个工具类来取出值

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
publicclass ConstantPropertiesUtil implements InitializingBean {

    @Value("${}")
    private String endpoint;

    @Value("${}")
    private String keyId;

    @Value("${}")
    private String keySecret;

    @Value("${}")
    private String bucketName;

    publicstatic String END_POINT;
    publicstatic String ACCESS_KEY_ID;
    publicstatic String ACCESS_KEY_SECRET;
    publicstatic String BUCKET_NAME;

    @Override
    publicvoid afterPropertiesSet() throws Exception {
        END_POINT = endpoint;
        ACCESS_KEY_ID = keyId;
        ACCESS_KEY_SECRET = keySecret;
        BUCKET_NAME = bucketName;
    }
}

然后使用swagger进行测试

673039ce9cb066226413d1ec5e0bb7a5.png

查看数据库

db08073190a9b08340e99c661e0dcea4.png

 存储成功

进入OSS文件目录中

b7f5002bdc33e1b9a906ed10e0b23442.png

 文件存储成功

这里前端我使用的是vue+element-ui

95ba4e5aeceef06a713f0b0353c9916e.png

 c5ea64f6b99a029dd9352d9893a74f3f.png

觉得有用的小伙伴记得点个收藏哦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值