Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
A
Afrishop refactored project
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Whispa
Afrishop refactored project
Commits
37e87b9b
提交
37e87b9b
authored
9月 12, 2020
作者:
Whispa
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commit
上级
0cd7de3d
隐藏空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
553 行增加
和
7 行删除
+553
-7
pom.xml
pom.xml
+8
-0
AuthController.java
...a/com/example/afrishop_v3/controllers/AuthController.java
+5
-5
FlutterWaveController.java
...xample/afrishop_v3/controllers/FlutterWaveController.java
+4
-1
ProblemController.java
...om/example/afrishop_v3/controllers/ProblemController.java
+1
-1
UploadController.java
...com/example/afrishop_v3/controllers/UploadController.java
+59
-0
UserController.java
...a/com/example/afrishop_v3/controllers/UserController.java
+55
-0
OssUtil.java
src/main/java/com/example/afrishop_v3/util/OssUtil.java
+416
-0
application.properties
src/main/resources/application.properties
+5
-0
没有找到文件。
pom.xml
浏览文件 @
37e87b9b
...
@@ -72,6 +72,14 @@
...
@@ -72,6 +72,14 @@
<artifactId>
staxon
</artifactId>
<artifactId>
staxon
</artifactId>
<version>
1.3
</version>
<version>
1.3
</version>
</dependency>
</dependency>
<!-- OSS-->
<dependency>
<groupId>
com.aliyun.oss
</groupId>
<artifactId>
aliyun-sdk-oss
</artifactId>
<version>
3.8.0
</version>
</dependency>
<dependency>
<dependency>
<groupId>
com.google.firebase
</groupId>
<groupId>
com.google.firebase
</groupId>
<artifactId>
firebase-admin
</artifactId>
<artifactId>
firebase-admin
</artifactId>
...
...
src/main/java/com/example/afrishop_v3/controllers/AuthController.java
浏览文件 @
37e87b9b
...
@@ -213,15 +213,15 @@ public class AuthController {
...
@@ -213,15 +213,15 @@ public class AuthController {
}
}
}
else
{
}
else
{
user
=
optional
.
get
();
user
=
optional
.
get
();
if
(
user
.
getPassword
()
==
null
){
user
.
setPassword
(
encoder
.
encode
(
user
.
getFirebaseUid
()));
// Assign user from database to the user we have to return back to request
userRepository
.
save
(
user
);
}
}
}
// generate token codes has been moved downwards from if condition of checking if user doesn't exist in database, because even if
// generate token codes has been moved downwards from if condition of checking if user doesn't exist in database, because even if
// user exist we have to generate token also
// user exist we have to generate token also
if
(
user
.
getPassword
()
==
null
){
user
.
setPassword
(
encoder
.
encode
(
user
.
getFirebaseUid
()));
// Assign user from database to the user we have to return back to request
}
user
.
setLastLoginTime
(
new
Date
());
userRepository
.
save
(
user
);
//注册成功 创建token
//注册成功 创建token
return
authenticateUser
(
new
LoginRequest
(
user
.
getFirebaseUid
(),
user
.
getFirebaseUid
()));
return
authenticateUser
(
new
LoginRequest
(
user
.
getFirebaseUid
(),
user
.
getFirebaseUid
()));
...
...
src/main/java/com/example/afrishop_v3/controllers/FlutterWaveController.java
浏览文件 @
37e87b9b
...
@@ -84,9 +84,12 @@ public class FlutterWaveController {
...
@@ -84,9 +84,12 @@ public class FlutterWaveController {
JSONObject
chargevisa
=
ch
.
chargeVisaAndIntl
();
JSONObject
chargevisa
=
ch
.
chargeVisaAndIntl
();
JSONObject
object
=
chargevisa
.
getJSONObject
(
"data"
);
JSONObject
object
=
chargevisa
.
getJSONObject
(
"data"
);
boolean
b
=
object
!=
null
&&
object
.
has
(
"authurl"
);
boolean
b
=
object
!=
null
&&
object
.
has
(
"authurl"
);
String
message
=
!
b
&&
object
!=
null
&&
object
.
has
(
"message"
)
?
object
.
getString
(
"message"
)
:
ResultCodeEnum
.
SERVICE_ERROR
.
getDesc
();
json
.
put
(
"data"
,
chargevisa
);
json
.
put
(
"data"
,
chargevisa
);
json
.
put
(
"code"
,
b
?
ResultCodeEnum
.
SUCCESS
.
getCode
()
:
ResultCodeEnum
.
SERVICE_ERROR
.
getCode
());
json
.
put
(
"code"
,
b
?
ResultCodeEnum
.
SUCCESS
.
getCode
()
:
ResultCodeEnum
.
SERVICE_ERROR
.
getCode
());
json
.
put
(
"message"
,
b
?
ResultCodeEnum
.
SUCCESS
.
getDesc
()
:
object
==
null
?
ResultCodeEnum
.
SERVICE_ERROR
.
getDesc
()
:
object
.
toString
()
);
json
.
put
(
"message"
,
b
?
ResultCodeEnum
.
SUCCESS
.
getDesc
()
:
message
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
json
.
put
(
"code"
,
ResultCodeEnum
.
ORDER_PAY_ERROR
.
getCode
()).
put
(
"message"
,
e
.
getMessage
());
json
.
put
(
"code"
,
ResultCodeEnum
.
ORDER_PAY_ERROR
.
getCode
()).
put
(
"message"
,
e
.
getMessage
());
logger
.
error
(
e
.
getMessage
(),
e
);
logger
.
error
(
e
.
getMessage
(),
e
);
...
...
src/main/java/com/example/afrishop_v3/controllers/ProblemController.java
浏览文件 @
37e87b9b
...
@@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.RequestParam;
...
@@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
@RestController
@RestController
@RequestMapping
(
"/
search
"
)
@RequestMapping
(
"/
problem
"
)
public
class
ProblemController
{
public
class
ProblemController
{
private
final
TbCfProblemRepository
repository
;
private
final
TbCfProblemRepository
repository
;
...
...
src/main/java/com/example/afrishop_v3/controllers/UploadController.java
0 → 100644
浏览文件 @
37e87b9b
package
com
.
example
.
afrishop_v3
.
controllers
;
import
com.example.afrishop_v3.base.Result
;
import
com.example.afrishop_v3.enums.ResultCodeEnum
;
import
com.example.afrishop_v3.util.HttpClientUtil
;
import
com.example.afrishop_v3.util.OssUtil
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @Auther: wudepeng
* @Date: 2020/01/09
* @Description:文件上传API
*/
@RestController
@RequestMapping
(
"/api/upload"
)
public
class
UploadController
{
@Value
(
"${upload.api}"
)
private
String
api
;
@PostMapping
(
"/uploadFile"
)
public
Result
uploadFile
(
@RequestBody
String
strImg
)
throws
Exception
{
Result
<
String
>
result
=
new
Result
<>();
try
{
if
(!
StringUtils
.
isBlank
(
strImg
))
{
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"file"
,
strImg
);
String
url
=
HttpClientUtil
.
sendPostWithBodyParameter
(
api
,
map
);
url
=
url
.
substring
(
1
,
url
.
length
()
-
1
);
result
.
setData
(
url
).
setMessage
(
ResultCodeEnum
.
SUCCESS
.
getDesc
());
}
else
{
result
.
setCode
(
ResultCodeEnum
.
SERVICE_ERROR
.
getCode
());
result
.
setMessage
(
"The parameter cannot be null"
);
}
}
catch
(
Exception
e
)
{
result
.
setCode
(
ResultCodeEnum
.
SERVICE_ERROR
.
getCode
());
result
.
setMessage
(
"Upload failed"
);
}
return
result
;
}
//@ApiOperation("删除文件")
@DeleteMapping
(
"/delFile"
)
public
Result
delFile
(
@RequestParam
(
"url"
)
String
url
)
{
Result
<
Map
<
String
,
Object
>>
result
=
new
Result
<>();
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
boolean
b
=
OssUtil
.
deleteFile
(
url
);
map
.
put
(
"isDeleted"
,
b
);
result
.
setData
(
map
).
setMessage
(
ResultCodeEnum
.
SUCCESS
.
getDesc
());
return
result
;
}
}
src/main/java/com/example/afrishop_v3/controllers/UserController.java
0 → 100644
浏览文件 @
37e87b9b
package
com
.
example
.
afrishop_v3
.
controllers
;
import
com.example.afrishop_v3.base.Result
;
import
com.example.afrishop_v3.enums.ResultCodeEnum
;
import
com.example.afrishop_v3.models.TbCfUserInfo
;
import
com.example.afrishop_v3.repository.UserRepository
;
import
com.example.afrishop_v3.security.services.AuthenticationUser
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.web.bind.annotation.PutMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
@RestController
@RequestMapping
(
"user"
)
public
class
UserController
{
private
final
UserRepository
repository
;
private
final
AuthenticationUser
user
;
public
UserController
(
UserRepository
repository
,
AuthenticationUser
user
)
{
this
.
repository
=
repository
;
this
.
user
=
user
;
}
@PutMapping
public
Result
updateUser
(
@RequestBody
TbCfUserInfo
info
)
{
TbCfUserInfo
user
=
this
.
user
.
user
();
if
(
info
==
null
)
return
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
"Empty body"
);
if
(
StringUtils
.
isNotBlank
(
info
.
getAvatar
())
){
user
.
setAvatar
(
info
.
getAvatar
());
}
if
(
StringUtils
.
isNotBlank
(
info
.
getBirthday
())
){
user
.
setBirthday
(
info
.
getBirthday
());
}
if
(
StringUtils
.
isNotBlank
(
info
.
getNick
())
){
user
.
setNick
(
info
.
getNick
());
}
if
(
info
.
getSex
()
!=
null
){
user
.
setSex
(
info
.
getSex
());
}
TbCfUserInfo
save
=
repository
.
save
(
user
);
return
new
Result
<>(
save
);
}
}
src/main/java/com/example/afrishop_v3/util/OssUtil.java
0 → 100644
浏览文件 @
37e87b9b
package
com
.
example
.
afrishop_v3
.
util
;
import
com.aliyun.oss.OSSClient
;
import
com.aliyun.oss.model.Bucket
;
import
com.aliyun.oss.model.OSSObject
;
import
com.aliyun.oss.model.ObjectMetadata
;
import
com.aliyun.oss.model.PutObjectResult
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.*
;
import
java.net.URL
;
import
java.util.Date
;
public
class
OssUtil
{
////////// 阿里云要求jdom使用1.1版本!!!!!!!!!!!!!
//阿里云API的内或外网域名
private
static
final
String
ENDPOINT
=
"oss-cn-shenzhen.aliyuncs.com"
;
//阿里云API的密钥Access Key ID
private
static
final
String
ACCESS_KEY_ID
=
"5d6h34keSd9K3l2x"
;
//阿里云API的密钥Access Key Secret
private
static
final
String
ACCESS_KEY_SECRET
=
"l2dJ4LpY6D6qV3jj3iQif7EPByoVQE"
;
//阿里云API的bucket名称
private
static
final
String
BACKET_NAME
=
"diaosaas-prod"
;
//阿里云API的文件夹名称
private
static
final
String
FOLDER
=
"africa-shop/"
;
// // 阿里云API的内或外网域名
// private static final String ENDPOINT = "oss-cn-shenzhen.aliyuncs.com";
// // 阿里云API的密钥Access Key ID
// private static final String ACCESS_KEY_ID = "5d6h34keSd9K3l2x";
// // 阿里云API的密钥Access Key Secret
// private static final String ACCESS_KEY_SECRET = "l2dJ4LpY6D6qV3jj3iQif7EPByoVQE";
// // 阿里云API的bucket名称
// private static final String BACKET_NAME = "diaosaas-prod";
// // 阿里云API的文件夹名称
// private static final String FOLDER = "wosenad/";
/**
* 获取阿里云OSS客户端对象
*
* @return
*/
public
static
OSSClient
getOSSClient
()
{
return
new
OSSClient
(
ENDPOINT
,
ACCESS_KEY_ID
,
ACCESS_KEY_SECRET
);
}
/**
* 创建存储空间
*
* @param ossClient
* @param bucketName
* @return
*/
public
static
String
createBucketName
(
OSSClient
ossClient
,
String
bucketName
)
{
// 存储空间
final
String
bucketNames
=
bucketName
;
if
(!
ossClient
.
doesBucketExist
(
bucketName
))
{
// 创建存储空间
Bucket
bucket
=
ossClient
.
createBucket
(
bucketName
);
return
bucket
.
getName
();
}
return
bucketNames
;
}
/**
* 删除存储空间
*
* @param ossClient
* @param bucketName
*/
public
static
void
deleteBucket
(
OSSClient
ossClient
,
String
bucketName
)
{
ossClient
.
deleteBucket
(
bucketName
);
}
/**
* 创建模拟文件夹
*
* @param ossClient
* oss连接
* @param bucketName
* 存储空间
* @param folder
* 模拟文件夹名如"wenjianjia/"
* @return
*/
public
static
String
createFolder
(
OSSClient
ossClient
,
String
bucketName
,
String
folder
)
{
// 文件夹名
final
String
keySuffixWithSlash
=
folder
;
// 判断文件夹是否存在,不存在则创建
if
(!
ossClient
.
doesObjectExist
(
bucketName
,
keySuffixWithSlash
))
{
// 创建文件夹
ossClient
.
putObject
(
bucketName
,
keySuffixWithSlash
,
new
ByteArrayInputStream
(
new
byte
[
0
]));
;
// 得到文件夹名
OSSObject
object
=
ossClient
.
getObject
(
bucketName
,
keySuffixWithSlash
);
String
fileDir
=
object
.
getKey
();
return
fileDir
;
}
return
keySuffixWithSlash
;
}
/**
* 根据key和folder删除OSS服务器上的文件
*
* @param bucketName
* bucketName存储空间
* @param folder
* folder模拟文件夹名 如"qj_nanjing/"
* @param key
* Bucket下的文件的路径名+文件名 如:"upload/cake.jpg"
*/
public
static
String
deleteFile
(
String
bucketName
,
String
folder
,
String
key
)
{
OSSClient
ossClient
=
getOSSClient
();
try
{
ossClient
.
deleteObject
(
bucketName
,
folder
+
key
);
return
"success"
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
"error"
;
}
finally
{
ossClient
.
shutdown
();
}
}
/**
* 根据key删除OSS服务器上的文件
*
* @param bucketName
* bucketName存储空间
* @param key
* Bucket下的文件的路径名+文件名 如:"upload/cake.jpg"
*/
public
static
String
deleteFile
(
String
bucketName
,
String
key
)
{
OSSClient
ossClient
=
getOSSClient
();
try
{
ossClient
.
deleteObject
(
bucketName
,
key
);
return
"success"
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
"error"
;
}
finally
{
ossClient
.
shutdown
();
}
}
/**
* 根据key删除OSS服务器上的文件
*
* @param key
* Bucket下的文件的路径名+文件名 如:"upload/cake.jpg" "http://" + BACKET_NAME +
* "." + ENDPOINT + "/" + FOLDER + secondaryFolderName + "/" +
* fileNameNew
*/
public
static
boolean
deleteFile
(
String
key
)
{
OSSClient
ossClient
=
getOSSClient
();
try
{
// 将图片路径前缀去除
String
pathPrefix
=
"https://"
+
BACKET_NAME
+
"."
+
ENDPOINT
+
"/"
;
String
filePath
=
key
.
startsWith
(
pathPrefix
)
?
key
.
substring
(
pathPrefix
.
length
(),
key
.
length
())
:
key
;
ossClient
.
deleteObject
(
BACKET_NAME
,
filePath
);
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
finally
{
ossClient
.
shutdown
();
}
}
/**
* 上传图片至OSS
*
* @param file
* @param secondaryFolderName
* 二级文件夹目录
* @return
*/
public
static
String
uploadObject2OSS
(
File
file
,
String
secondaryFolderName
)
{
OSSClient
ossClient
=
getOSSClient
();
try
{
// 以输入流的形式上传文件
InputStream
is
=
new
FileInputStream
(
file
);
String
path
=
file
.
getName
();
String
fileName
=
path
.
substring
(
path
.
lastIndexOf
(
"//"
)
+
1
);
String
type
=
path
.
substring
(
path
.
lastIndexOf
(
"."
)
+
1
);
int
random
=
(
int
)
(
Math
.
random
()
*
900
)
+
10000
;
// 文件名
String
fileNameNew
=
fileName
.
replace
(
fileName
,
String
.
valueOf
(
System
.
currentTimeMillis
())
+
String
.
valueOf
(
random
)
+
"."
+
type
);
// 文件大小
Long
fileSize
=
file
.
length
();
// 创建上传Object的Metadata
ObjectMetadata
metadata
=
new
ObjectMetadata
();
// 上传的文件的长度
metadata
.
setContentLength
(
is
.
available
());
// 指定该Object被下载时的网页的缓存行为
metadata
.
setCacheControl
(
"no-cache"
);
// 指定该Object下设置Header
metadata
.
setHeader
(
"Pragma"
,
"no-cache"
);
// 指定该Object被下载时的内容编码格式
metadata
.
setContentEncoding
(
"utf-8"
);
// 文件的MIME,定义文件的类型及网页编码,决定浏览器将以什么形式、什么编码读取文件。如果用户没有指定则根据Key或文件名的扩展名生成,
// 如果没有扩展名则填默认值application/octet-stream
metadata
.
setContentType
(
getContentType
(
fileNameNew
));
// 指定该Object被下载时的名称(指示MINME用户代理如何显示附加的文件,打开或下载,及文件名称)
metadata
.
setContentDisposition
(
"filename/filesize="
+
fileNameNew
+
"/"
+
fileSize
+
"Byte."
);
//System.out.println(FOLDER + fileNameNew);
// 上传文件 (上传文件流的形式)
PutObjectResult
putResult
=
ossClient
.
putObject
(
BACKET_NAME
,
FOLDER
+
secondaryFolderName
+
"/"
+
fileNameNew
,
is
,
metadata
);
// 解析结果
URL
url
=
getUrl
(
ossClient
,
putResult
.
getETag
());
return
"https://"
+
BACKET_NAME
+
"."
+
ENDPOINT
+
"/"
+
FOLDER
+
secondaryFolderName
+
"/"
+
fileNameNew
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
"error"
;
}
finally
{
ossClient
.
shutdown
();
}
// return url;
}
/**
* 通过文件名判断并获取OSS服务文件上传时文件的contentType
*
* @param fileName
* @return
*/
public
static
String
getContentType
(
String
fileName
)
{
// 文件的后缀名
String
fileExtension
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
));
if
(
".bmp"
.
equalsIgnoreCase
(
fileExtension
))
{
return
"image/bmp"
;
}
if
(
".gif"
.
equalsIgnoreCase
(
fileExtension
))
{
return
"image/gif"
;
}
if
(
".jpeg"
.
equalsIgnoreCase
(
fileExtension
)
||
".jpg"
.
equalsIgnoreCase
(
fileExtension
)
||
".png"
.
equalsIgnoreCase
(
fileExtension
))
{
return
"image/jpeg"
;
}
if
(
".html"
.
equalsIgnoreCase
(
fileExtension
))
{
return
"text/html"
;
}
if
(
".txt"
.
equalsIgnoreCase
(
fileExtension
))
{
return
"text/plain"
;
}
if
(
".vsd"
.
equalsIgnoreCase
(
fileExtension
))
{
return
"application/vnd.visio"
;
}
if
(
".ppt"
.
equalsIgnoreCase
(
fileExtension
)
||
"pptx"
.
equalsIgnoreCase
(
fileExtension
))
{
return
"application/vnd.ms-powerpoint"
;
}
if
(
".doc"
.
equalsIgnoreCase
(
fileExtension
)
||
"docx"
.
equalsIgnoreCase
(
fileExtension
))
{
return
"application/msword"
;
}
if
(
".xml"
.
equalsIgnoreCase
(
fileExtension
))
{
return
"text/xml"
;
}
// 默认返回类型
return
"image/jpeg"
;
}
/**
*
* @param ossClient
* @param key
* @return
*/
public
static
URL
getUrl
(
OSSClient
ossClient
,
String
key
)
{
// 设置URL过期时间为10年
Date
expiration
=
new
Date
(
new
Date
().
getTime
()
+
3600
l
*
1000
*
24
*
365
*
10
);
// 生成URL
URL
url
=
ossClient
.
generatePresignedUrl
(
BACKET_NAME
,
key
,
expiration
);
return
url
;
}
/**
* MultipartFile 形式上传文件
* @param file
* @return
* @throws Exception
*/
public
static
String
upload
(
MultipartFile
file
)
throws
Exception
{
String
fileName
=
file
.
getOriginalFilename
();
String
prefix
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
));
final
File
realfile
=
File
.
createTempFile
(
IdUtil
.
createIdbyUUID
(),
prefix
);
file
.
transferTo
(
realfile
);
String
url
=
OssUtil
.
uploadObject2OSS
(
realfile
,
""
);
//System.out.println(url);
return
url
;
}
/**
* MultipartFile 形式上传文件
* @param file
* @return
* @throws Exception
*/
public
static
String
upload
(
MultipartFile
file
,
String
secondaryFolderName
)
throws
Exception
{
String
fileName
=
file
.
getOriginalFilename
();
String
prefix
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
));
final
File
realfile
=
File
.
createTempFile
(
IdUtil
.
createIdbyUUID
(),
prefix
);
file
.
transferTo
(
realfile
);
String
url
=
OssUtil
.
uploadObject2OSS
(
realfile
,
secondaryFolderName
);
//System.out.println(url);
return
url
;
}
/**
* MultipartFile 形式上传文件
* @param file
* @return
* @throws Exception
*/
public
static
String
upload
(
MultipartFile
file
,
String
fileName
,
String
secondaryFolderName
)
throws
Exception
{
String
prefix
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
)
).
toLowerCase
();
final
File
realfile
=
File
.
createTempFile
(
fileName
,
prefix
);
file
.
transferTo
(
realfile
);
String
url
=
OssUtil
.
uploadObject2OSS
(
realfile
,
secondaryFolderName
);
//System.out.println(url);
return
url
;
}
/**
* MultipartFile 形式上传文件
* @param realfile
* @return
* @throws Exception
*/
public
static
String
upload
(
File
realfile
,
String
secondaryFolderName
)
{
String
url
=
OssUtil
.
uploadObject2OSS
(
realfile
,
secondaryFolderName
);
return
url
;
}
/**
* 以byte数组上传到oss
* @param fbytes byte数组
* @param fileName 文件名称
* @param secondaryFolderName 二级文件夹目录
* @return
* @throws Exception
*/
public
static
String
upload
(
byte
[]
fbytes
,
String
fileName
,
String
secondaryFolderName
)
throws
Exception
{
OSSClient
ossClient
=
getOSSClient
();
// String fileNameNew = "1.jpg";
// 上传Byte数组。
PutObjectResult
putResult
=
ossClient
.
putObject
(
BACKET_NAME
,
FOLDER
+
secondaryFolderName
+
"/"
+
fileName
,
new
ByteArrayInputStream
(
fbytes
));
// 解析结果
URL
url
=
getUrl
(
ossClient
,
putResult
.
getETag
());
// System.out.println(url);
// 关闭OSSClient。
ossClient
.
shutdown
();
return
"https://"
+
BACKET_NAME
+
"."
+
ENDPOINT
+
"/"
+
FOLDER
+
secondaryFolderName
+
"/"
+
fileName
;
}
/**
* 将文件转换成byte数组
* @param tradeFile
* @return
*/
public
static
byte
[]
File2byte
(
File
tradeFile
){
byte
[]
buffer
=
null
;
try
{
FileInputStream
fis
=
new
FileInputStream
(
tradeFile
);
ByteArrayOutputStream
bos
=
new
ByteArrayOutputStream
();
byte
[]
b
=
new
byte
[
1024
];
int
n
;
while
((
n
=
fis
.
read
(
b
))
!=
-
1
)
{
bos
.
write
(
b
,
0
,
n
);
}
fis
.
close
();
bos
.
close
();
buffer
=
bos
.
toByteArray
();
}
catch
(
FileNotFoundException
e
){
e
.
printStackTrace
();
}
catch
(
IOException
e
){
e
.
printStackTrace
();
}
return
buffer
;
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
boolean
flag
=
deleteFile
(
"http://guangzhou-douli.oss-cn-shenzhen.aliyuncs.com/shouli/qcrod/2.jpg"
);
//System.out.println(flag);
File
file
=
new
File
(
"C:\\Users\\yangshanlin\\Desktop\\bg-sfxt.jpg"
);
byte
[]
b
=
File2byte
(
file
);
//System.out.println(b);
String
s
=
upload
(
b
,
"3.jpg"
,
"qcrod"
);
//System.out.println(s);
}
public
static
State
uploadToAliOss
(
byte
[]
data
,
String
fileName
,
String
secondaryFolderName
)
throws
Exception
{
String
path
=
upload
(
data
,
fileName
,
secondaryFolderName
);
State
state
=
new
BaseState
(
true
,
path
);
state
.
putInfo
(
"size"
,
data
.
length
);
state
.
putInfo
(
"title"
,
fileName
);
state
.
putInfo
(
"url"
,
path
);
return
state
;
}
public
static
State
uploadFileToAliOss
(
File
realfile
,
String
secondaryFolderName
)
{
String
url
=
OssUtil
.
uploadObject2OSS
(
realfile
,
secondaryFolderName
);
State
state
=
new
BaseState
(
true
,
url
);
state
.
putInfo
(
"title"
,
realfile
.
getName
());
state
.
putInfo
(
"url"
,
url
);
return
state
;
}
}
src/main/resources/application.properties
浏览文件 @
37e87b9b
...
@@ -22,3 +22,8 @@ spring.servlet.multipart.max-request-size=456128KB
...
@@ -22,3 +22,8 @@ spring.servlet.multipart.max-request-size=456128KB
# App Properties
# App Properties
bezkoder.app.jwtSecret
=
bezKoderSecretKey
bezkoder.app.jwtSecret
=
bezKoderSecretKey
bezkoder.app.jwtExpirationMs
=
86400000
bezkoder.app.jwtExpirationMs
=
86400000
upload.api
=
http://dev.diaosaas.com:8302/africa-shop/api/upload/uploadBase64
# upload.api = http://admin.afrieshop.com/api/upload/uploadBase64
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论