springboot+vue實(shí)現(xiàn)oss文件存儲(chǔ)的示例代碼
前提oss準(zhǔn)備工作
進(jìn)入阿里云官網(wǎng):阿里云oss官網(wǎng)
注冊(cè)

搜OSS,點(diǎn)擊“對(duì)象存儲(chǔ)OSS”
第一次進(jìn)入需要開(kāi)通,直接點(diǎn)擊立即開(kāi)通,到右上角AccessKey管理中創(chuàng)建AccessKey,并且記住自己的accessKeyId和accessKeySecret,因?yàn)橹笠玫?,如果現(xiàn)在不記之后就會(huì)被隱藏


點(diǎn)擊右上角進(jìn)入自己賬號(hào)主頁(yè),鼠標(biāo)移到左上角三條杠會(huì)有側(cè)邊欄彈出,找到對(duì)象存儲(chǔ)OSS,開(kāi)通后,創(chuàng)建Bucket。名稱和地域自己填寫(xiě)和選擇,其他默認(rèn)不變,讀寫(xiě)權(quán)限為“公共讀”,其余默認(rèn)即可


添加依賴
阿里云的oss依賴
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.1</version>
</dependency>
AliOSS工具類
package com.wedu.modules.tain.utils;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
/**
* 阿里云 OSS 工具類
*/
@Data
@ConfigurationProperties(prefix = "aliyun3.oss")
@Component
public class AliOSSUtil {
// 這些成員變量等會(huì)在配置項(xiàng)中設(shè)置
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
/**
* 實(shí)現(xiàn)上傳圖片到OSS
*/
public String upload(MultipartFile file) throws IOException {
// 獲取上傳的文件的輸入流
InputStream inputStream = file.getInputStream();
// 避免文件覆蓋(一面文件重名時(shí),上傳失?。?
String originalFilename = file.getOriginalFilename();
String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//上傳文件到 OSS
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ossClient.putObject(bucketName, fileName, inputStream);
//文件訪問(wèn)路徑
String url = endpoint.split("http://")[0] + "http://" + bucketName + "." + endpoint.split("http://")[1] + "/" + fileName;
// 關(guān)閉ossClient
ossClient.shutdown();
return url;// 把上傳到oss的路徑返回
}
}
controller類:接收請(qǐng)求,返回文件路徑
@Autowired
private AliOSSUtil aliOSSUtil;
//oss
@PostMapping("/ossUpload")
public R ossUpload(MultipartFile file) throws IOException {
String url = aliOSSUtil.upload(file); // 返回文件的上傳路徑,訪問(wèn)這個(gè)url即可下載
return R.ok().put("url",url);
}
更新application.yml配置
# 阿里云OSS配置 wyj配置
aliyun3:
oss:
endpoint: http://見(jiàn)下文解說(shuō)
accessKeyId: 自己的KeyId
accessKeySecret: 自己的KeySecret
bucketName: 創(chuàng)建的bucket的名稱
外網(wǎng)訪問(wèn)的那個(gè)東東復(fù)制在http:后面就行

postman測(cè)試
測(cè)試成功

列表里也有剛剛上傳的數(shù)據(jù)

vue
首先在頁(yè)面上寫(xiě)一個(gè)文件上傳按鈕,點(diǎn)擊則彈出彈窗,彈窗內(nèi)再繼續(xù)寫(xiě)文件上傳
<template>
<div>
<el-form :inline="true">
<el-form-item>
<el-button
type="primary"
@click="fileUpload()"
>文件上傳</el-button
>
</el-form-item>
</el-form>
<file-upload v-if="fileUploadVisible" ref="FileUpload"></file-upload>
</div>
</template>
<script>
import FileUpload from "./warn-file-upload";
export default {
data() {
return {
fileUploadVisible:false
};
},
components: {
FileUpload,
},
methods: {
fileUpload(){
this.fileUploadVisible = true;
this.$nextTick(() => {
this.$refs.FileUpload.init();
});
},
},
};
</script>
<style>
</style>
彈窗內(nèi)寫(xiě)一個(gè)下拉選擇框,后續(xù)還會(huì)寫(xiě)本地存儲(chǔ)以及minio
<template>
<el-dialog
title="文件傳輸"
:visible.sync="visible"
:close-on-click-modal="false"
>
<el-select v-model="value" placeholder="請(qǐng)選擇存儲(chǔ)方式">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
<el-upload
class="upload-demo"
drag
:action="getUploadAction(this.value)"
:headers="tokenInfo"
multiple
:disabled="!this.value"
>
<i class="el-icon-upload"></i>
<div class="el-upload__text">將文件拖到此處,或<em>點(diǎn)擊上傳</em></div>
<div class="el-upload__tip" slot="tip">
請(qǐng)先選擇上傳方式再進(jìn)行文件上傳
</div>
</el-upload>
</el-dialog>
</template>
<script>
export default {
data() {
return {
ossUpload: this.$http.adornUrl("/tain/warn/ossUpload"),
tokenInfo: {
token: this.$cookie.get("token"),
},
visible: true,
options: [
{
value: "1",
label: "本地存儲(chǔ)",
},
],
value: "",
};
},
methods: {
init() {
this.visible = true;
this.$nextTick(() => {
this.value = "";
});
},
getUploadAction(value) {
if (value == 1) {
return this.ossUpload;
} else if (value == "") {
return this.value;
}
},
},
};
</script>
<style>
</style>到此這篇關(guān)于springboot+vue實(shí)現(xiàn)oss文件存儲(chǔ)的示例代碼的文章就介紹到這了,更多相關(guān)springboot oss文件存儲(chǔ)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC互聯(lián)網(wǎng)軟件架構(gòu)REST使用詳解
這篇文章主要為大家詳細(xì)介紹了SpringMVC互聯(lián)網(wǎng)軟件架構(gòu)REST的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
jsp+servlet實(shí)現(xiàn)簡(jiǎn)單登錄頁(yè)面功能(附demo)
本文主要介紹了jsp+servlet實(shí)現(xiàn)簡(jiǎn)單登錄頁(yè)面功能登錄成功跳轉(zhuǎn)新頁(yè)面,登錄失敗在原登錄界面提示登錄失敗信息,對(duì)初學(xué)者有一定的幫助,感興趣的可以了解一下2021-07-07
Spring Boot中使用Redis做緩存的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Spring Boot中使用Redis做緩存的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06
解決Java執(zhí)行Cmd命令出現(xiàn)的死鎖問(wèn)題
這篇文章主要介紹了關(guān)于Java執(zhí)行Cmd命令出現(xiàn)的死鎖問(wèn)題解決,解決方法就是在waitfor()方法之前讀出窗口的標(biāo)準(zhǔn)輸出、輸出、錯(cuò)誤緩沖區(qū)中的內(nèi)容,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
Java NumberFormat格式化float類型的bug
今天小編就為大家分享一篇關(guān)于Java NumberFormat格式化float類型的bug,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
dubbo環(huán)境搭建ZooKeeper注冊(cè)中心全過(guò)程
文章介紹Dubbo環(huán)境搭建,包含ZooKeeper注冊(cè)中心的安裝配置(修改數(shù)據(jù)存儲(chǔ)路徑、啟動(dòng)服務(wù)并驗(yàn)證節(jié)點(diǎn))和dubbo-admin監(jiān)控中心的部署流程(解壓、打包、運(yùn)行jar包,需注意Maven版本及zkServer狀態(tài))2025-07-07
Spring中的注解@Value("#{}")與@Value("${}")的區(qū)別
這篇文章主要介紹了Spring中的注解@Value(“#{}“)與@Value(“${}“)的區(qū)別到底是什么,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06

