Java使用OSS實現(xiàn)上傳文件功能
1.準備OSS
- 訪問阿里云官網(wǎng),注冊并登錄賬戶(支付寶方便一些,順便沖點錢)
- 在產(chǎn)品分類中,找到阿里云oss
- 然后創(chuàng)建一個bucket

2.使用OSS(上傳文件)
2.1在阿里云網(wǎng)站導(dǎo)出阿里云頒發(fā)的 id 和 秘鑰
2.2導(dǎo)入maven坐標(biāo)
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.1.0</version>
</dependency>
2.3進入阿里云oss的學(xué)習(xí)路徑,找到對應(yīng)java操作的代碼
2.3.1首先創(chuàng)建配置文件,將秘鑰等信息進行配置
#服務(wù)端口 server.port=8002 #服務(wù)名 spring.application.name=service-oss #環(huán)境設(shè)置:dev、test、prod spring.profiles.active=dev #阿里云 OSS #不同的服務(wù)器,地址不同 aliyun.oss.file.endpoint=oss-cn-beijing.aliyuncs.com aliyun.oss.file.keyid=自己的keyid aliyun.oss.file.keysecret=自己的keysecret #bucket可以在控制臺創(chuàng)建,也可以使用java代碼創(chuàng)建 aliyun.oss.file.bucketname=自己創(chuàng)建的bucket名字
2.3.2創(chuàng)建一個工具類,將配置的屬性注入到容器中

package com.gyb.eduoss.utils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Author: 郜宇博
* @Date: 2021/9/30 14:54
*/
@Component
@ConfigurationProperties(prefix = "aliyun.oss.file")
@Data
public class AliyunOSSProUtil {
private String endpoint;
private String keyid;
private String keysecret;
private String bucketname;
}
2.3.3編寫java實現(xiàn)類代碼(文件上傳)
package com.gyb.eduoss.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.gyb.eduoss.service.OssService;
import com.gyb.eduoss.utils.AliyunOSSProUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
/**
* @Author: 郜宇博
* @Date: 2021/9/30 15:53
*/
@Service
public class OssServiceImpl implements OssService {
@Autowired
AliyunOSSProUtil aliyunOSSProUtil;
@Override
public String uploadFileAvatar(MultipartFile multipartFile) {
//從工具類獲取配置文件中的oss值
String endpoint = aliyunOSSProUtil.getEndpoint();
String accessKeyId = aliyunOSSProUtil.getKeyid();
String accessKeySecret = aliyunOSSProUtil.getKeysecret();
String bucketName = aliyunOSSProUtil.getBucketname();
System.out.println("endponit:"+endpoint);
// 創(chuàng)建OSSClient實例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 填寫本地文件的完整路徑。
// 如果未指定本地路徑,則默認從示例程序所屬項目對應(yīng)本地路徑中上傳文件流。
InputStream inputStream;
try {
inputStream = multipartFile.getInputStream();
//參數(shù)一:依次填寫B(tài)ucket名稱
// 參數(shù)二:文件名稱,或Object完整路徑(例如exampledir/exampleobject.txt)。Object完整路徑中不能包含Bucket名稱。
// 參數(shù)三:輸入流
String currentTime = String.valueOf(System.currentTimeMillis());
String currentDate = new DateTime().toString("yyyy/MM/dd");
String originalFileName = multipartFile.getOriginalFilename();
String fileName = currentDate+"/"+currentTime+originalFileName;
ossClient.putObject(bucketName,fileName, inputStream);
String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
return url;
} catch (IOException e) {
e.printStackTrace();
}finally {
// 關(guān)閉OSSClient。
ossClient.shutdown();
}
return null;
}
}
2.3.4創(chuàng)建config類,防止傳入文件為null
package com.gyb.eduoss.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
/**
* @Author: 郜宇博
* @Date: 2021/10/2 00:21
*/
@Configuration
public class UploadConfig {
@Bean(name = "multipartResolver")
public MultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
//resolveLazily屬性啟用是為了推遲文件解析,以在在UploadAction中捕獲文件大小異常
resolver.setResolveLazily(true);
resolver.setMaxInMemorySize(40960);
//上傳文件大小 5M 5*1024*1024
resolver.setMaxUploadSize(5 * 1024 * 1024);
return resolver;
}
}
3.配置Nginx反向代理(可以不配)
下載Nginx
實現(xiàn)將對應(yīng)訪問地址進行正則匹配轉(zhuǎn)發(fā)

3.1將監(jiān)聽端口改為81(防止沖突)

3.2配置轉(zhuǎn)發(fā)列表

server {
listen 9001;
server_name localhost;
#代表帶有eduservice路徑的請求都走http://localhost:8001
location ~ /eduservice/ {
proxy_pass http://localhost:8001;
}
#代表帶有eduoss的請求都走http://localhost:8002
location ~ /eduoss/ {
proxy_pass http://localhost:8002;
}
}
4.結(jié)果
此時將前端的訪問接口改為9001,即可實現(xiàn)下圖功能
從而實現(xiàn)功能不同的請求訪問不同的服務(wù)器


以上就是Java使用OSS實現(xiàn)上傳文件功能的詳細內(nèi)容,更多關(guān)于Java OSS上傳文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring Boot集成springfox-swagger2構(gòu)建restful API的方法教程
這篇文章主要給大家介紹了關(guān)于Spring Boot集成springfox-swagger2構(gòu)建restful API的相關(guān)資料,文中介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2017-06-06
Spring MVC 關(guān)于controller的字符編碼問題
在使用springMVC框架構(gòu)建web應(yīng)用,客戶端常會請求字符串、整型、json等格式的數(shù)據(jù),通常使用@ResponseBody注解使 controller回應(yīng)相應(yīng)的數(shù)據(jù)而不是去渲染某個頁面。2017-03-03
SpringBoot在自定義類中調(diào)用service層等Spring其他層操作
這篇文章主要介紹了SpringBoot在自定義類中調(diào)用service層等Spring其他層操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringBoot配置默認HikariCP數(shù)據(jù)源
咱們開發(fā)項目的過程中用到很多的開源數(shù)據(jù)庫鏈接池,比如druid、c3p0、BoneCP等等,本文主要介紹了SpringBoot配置默認HikariCP數(shù)據(jù)源,具有一定的參考價值,感興趣的可以了解一下2023-11-11
JSON序列化導(dǎo)致Long類型被搞成Integer的坑及解決
這篇文章主要介紹了JSON序列化導(dǎo)致Long類型被搞成Integer的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Spring Cloud超詳細i講解Feign自定義配置與使用
這篇文章主要介紹了SpringCloud Feign自定義配置與使用,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
Java解決xss轉(zhuǎn)義導(dǎo)致轉(zhuǎn)碼的問題
跨站腳本攻擊XSS是最普遍的Web應(yīng)用安全漏洞,本文主要介紹了Java解決xss轉(zhuǎn)義導(dǎo)致轉(zhuǎn)碼的問題,具有一定的參考價值,感興趣的可以了解一下2023-08-08

