Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
open-source
/
rpc-openplatform
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 0be2d805
authored
Dec 21, 2020
by
tyfanchz
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
增加文件上传类
1 parent
2931efd4
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
141 additions
and
11 deletions
rpc-openplatform-api/pom.xml
rpc-openplatform-api/src/main/java/cn/com/glsx/openplatform/config/FdfsProperty.java
rpc-openplatform-api/src/main/java/cn/com/glsx/openplatform/enums/ResultCodeEnum.java
rpc-openplatform-api/src/main/java/cn/com/glsx/openplatform/util/FdfsUtils.java
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/util/RSAUtils.java → rpc-openplatform-api/src/main/java/cn/com/glsx/openplatform/util/RSAUtils.java
rpc-openplatform-api/pom.xml
View file @
0be2d80
...
@@ -60,6 +60,23 @@
...
@@ -60,6 +60,23 @@
<artifactId>
spring-data-commons
</artifactId>
<artifactId>
spring-data-commons
</artifactId>
<version>
2.4.2
</version>
<version>
2.4.2
</version>
</dependency>
</dependency>
<dependency>
<groupId>
com.github.tobato
</groupId>
<artifactId>
fastdfs-client
</artifactId>
<version>
1.27.2
</version>
<exclusions>
<exclusion>
<groupId>
org.slf4j
</groupId>
<artifactId>
*
</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-web
</artifactId>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
rpc-openplatform-api/src/main/java/cn/com/glsx/openplatform/config/FdfsProperty.java
0 → 100644
View file @
0be2d80
package
cn
.
com
.
glsx
.
openplatform
.
config
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.context.annotation.Configuration
;
/**
* <p>Description: FastDFS 配置类
* <p>Project: rpc-openplatform-container
*
* @author wbh
* @date 2020-12-19
*/
@Configuration
@ConfigurationProperties
(
prefix
=
"fdfs"
)
public
class
FdfsProperty
{
private
String
trackerNginxDomain
;
public
String
getTrackerNginxDomain
()
{
return
trackerNginxDomain
;
}
public
void
setTrackerNginxDomain
(
String
trackerNginxDomain
)
{
this
.
trackerNginxDomain
=
trackerNginxDomain
;
}
}
rpc-openplatform-api/src/main/java/cn/com/glsx/openplatform/enums/ResultCodeEnum.java
View file @
0be2d80
...
@@ -15,6 +15,8 @@ public enum ResultCodeEnum {
...
@@ -15,6 +15,8 @@ public enum ResultCodeEnum {
REQUEST_ERROR
(
401
,
"请求错误"
),
REQUEST_ERROR
(
401
,
"请求错误"
),
PARAM_ERROR
(
402
,
"参数错误"
),
PARAM_ERROR
(
402
,
"参数错误"
),
SERVER_ERROR
(
500
,
"系统异常"
),
SERVER_ERROR
(
500
,
"系统异常"
),
FILE_UPLOAD_ERROR
(
501
,
"文件上传失败"
),
FILE_DELETE_ERROR
(
502
,
"文件删除失败"
),
;
;
private
final
Integer
code
;
private
final
Integer
code
;
...
...
rpc-openplatform-api/src/main/java/cn/com/glsx/openplatform/util/FdfsUtils.java
0 → 100644
View file @
0be2d80
package
cn
.
com
.
glsx
.
openplatform
.
util
;
import
java.io.ByteArrayInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
cn.com.glsx.openplatform.config.FdfsProperty
;
import
com.github.tobato.fastdfs.domain.fdfs.StorePath
;
import
com.github.tobato.fastdfs.service.FastFileStorageClient
;
import
org.apache.commons.io.FilenameUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.multipart.MultipartFile
;
/**
* <p>Description: FastDFS 文件操作工具类
* <p>Project: shop
*
* @author wbh
* @date 2020-12-15
*/
@Component
public
class
FdfsUtils
{
private
static
final
String
FDFS_DOMAIN_PROP
=
"tracker-nginx-domain"
;
private
static
final
String
DOMAIN_SEPARATOR
=
"/"
;
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
FdfsUtils
.
class
);
private
static
FdfsProperty
fdfsProperty
;
private
static
FastFileStorageClient
storageClient
;
public
static
String
uploadFile
(
MultipartFile
multipartFile
)
throws
IOException
{
String
fileUrl
;
InputStream
inputStream
=
multipartFile
.
getInputStream
();
long
fileSize
=
multipartFile
.
getSize
();
String
fileExtension
=
FilenameUtils
.
getExtension
(
multipartFile
.
getOriginalFilename
());
StorePath
storePath
=
storageClient
.
uploadFile
(
inputStream
,
fileSize
,
fileExtension
,
null
);
fileUrl
=
getFileUrl
(
storePath
);
return
fileUrl
;
}
public
static
String
uploadFile
(
byte
[]
bytes
,
String
fileExtension
)
{
ByteArrayInputStream
inputStream
=
new
ByteArrayInputStream
(
bytes
);
StorePath
storePath
=
storageClient
.
uploadFile
(
inputStream
,
bytes
.
length
,
fileExtension
,
null
);
String
fileUrl
=
getFileUrl
(
storePath
);
return
fileUrl
;
}
public
static
String
uploadFile
(
InputStream
inputStream
,
String
fileExtension
)
throws
IOException
{
StorePath
storePath
=
storageClient
.
uploadFile
(
inputStream
,
inputStream
.
available
(),
fileExtension
,
null
);
String
fileUrl
=
getFileUrl
(
storePath
);
return
fileUrl
;
}
public
static
void
deleteFile
(
String
fileUrl
)
{
if
(
StringUtils
.
isNoneBlank
(
fileUrl
))
{
StorePath
storePath
=
StorePath
.
parseFromUrl
(
fileUrl
);
storageClient
.
deleteFile
(
storePath
.
getGroup
(),
storePath
.
getPath
());
}
}
private
static
String
getFileUrl
(
StorePath
storePath
)
{
String
domain
=
fdfsProperty
.
getTrackerNginxDomain
();
String
fileUrl
=
domain
+
DOMAIN_SEPARATOR
+
storePath
.
getFullPath
();
return
fileUrl
;
}
@Autowired
public
void
setFdfsProperty
(
FdfsProperty
fdfsProperty
)
{
FdfsUtils
.
fdfsProperty
=
fdfsProperty
;
}
@Autowired
public
void
setStorageClient
(
FastFileStorageClient
storageClient
)
{
FdfsUtils
.
storageClient
=
storageClient
;
}
}
rpc-openplatform-
container
/src/main/java/cn/com/glsx/openplatform/util/RSAUtils.java
→
rpc-openplatform-
api
/src/main/java/cn/com/glsx/openplatform/util/RSAUtils.java
View file @
0be2d80
package
cn
.
com
.
glsx
.
openplatform
.
util
;
package
cn
.
com
.
glsx
.
openplatform
.
util
;
import
org.apache.commons.codec.binary.Base64
;
import
org.springframework.stereotype.Component
;
import
javax.crypto.Cipher
;
import
javax.crypto.Cipher
;
import
java.security.*
;
import
java.security.KeyFactory
;
import
java.security.KeyPair
;
import
java.security.KeyPairGenerator
;
import
java.security.NoSuchAlgorithmException
;
import
java.security.SecureRandom
;
import
java.security.interfaces.RSAPrivateKey
;
import
java.security.interfaces.RSAPrivateKey
;
import
java.security.interfaces.RSAPublicKey
;
import
java.security.interfaces.RSAPublicKey
;
import
java.security.spec.PKCS8EncodedKeySpec
;
import
java.security.spec.PKCS8EncodedKeySpec
;
import
java.security.spec.X509EncodedKeySpec
;
import
java.security.spec.X509EncodedKeySpec
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Map
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.Base64Utils
;
/**
/**
...
@@ -20,7 +23,7 @@ import java.util.Map;
...
@@ -20,7 +23,7 @@ import java.util.Map;
*/
*/
@Component
@Component
public
class
RSAUtils
{
public
class
RSAUtils
{
private
static
Map
<
Integer
,
String
>
keyMap
=
new
HashMap
<
Integer
,
String
>();
//用于封装随机产生的公钥与私钥
private
static
Map
<
Integer
,
String
>
keyMap
=
new
HashMap
<>();
//用于封装随机产生的公钥与私钥
public
static
void
main
(
String
[]
args
)
throws
Exception
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
//生成公钥和私钥
//生成公钥和私钥
...
@@ -52,9 +55,9 @@ public class RSAUtils {
...
@@ -52,9 +55,9 @@ public class RSAUtils {
KeyPair
keyPair
=
keyPairGen
.
generateKeyPair
();
KeyPair
keyPair
=
keyPairGen
.
generateKeyPair
();
RSAPrivateKey
privateKey
=
(
RSAPrivateKey
)
keyPair
.
getPrivate
();
// 得到私钥
RSAPrivateKey
privateKey
=
(
RSAPrivateKey
)
keyPair
.
getPrivate
();
// 得到私钥
RSAPublicKey
publicKey
=
(
RSAPublicKey
)
keyPair
.
getPublic
();
// 得到公钥
RSAPublicKey
publicKey
=
(
RSAPublicKey
)
keyPair
.
getPublic
();
// 得到公钥
String
publicKeyString
=
new
String
(
Base64
.
encodeBase64
(
publicKey
.
getEncoded
()
));
String
publicKeyString
=
Base64Utils
.
encodeToString
(
publicKey
.
getEncoded
(
));
// 得到私钥字符串
// 得到私钥字符串
String
privateKeyString
=
new
String
(
Base64
.
encodeBase64
((
privateKey
.
getEncoded
())
));
String
privateKeyString
=
Base64Utils
.
encodeToString
(
privateKey
.
getEncoded
(
));
// 将公钥和私钥保存到Map
// 将公钥和私钥保存到Map
keyMap
.
put
(
0
,
publicKeyString
);
//0表示公钥
keyMap
.
put
(
0
,
publicKeyString
);
//0表示公钥
keyMap
.
put
(
1
,
privateKeyString
);
//1表示私钥
keyMap
.
put
(
1
,
privateKeyString
);
//1表示私钥
...
@@ -71,12 +74,12 @@ public class RSAUtils {
...
@@ -71,12 +74,12 @@ public class RSAUtils {
*/
*/
public
static
String
encrypt
(
String
str
,
String
publicKey
)
throws
Exception
{
public
static
String
encrypt
(
String
str
,
String
publicKey
)
throws
Exception
{
//base64编码的公钥
//base64编码的公钥
byte
[]
decoded
=
Base64
.
decodeBase64
(
publicKey
);
byte
[]
decoded
=
Base64
Utils
.
decodeFromString
(
publicKey
);
RSAPublicKey
pubKey
=
(
RSAPublicKey
)
KeyFactory
.
getInstance
(
"RSA"
).
generatePublic
(
new
X509EncodedKeySpec
(
decoded
));
RSAPublicKey
pubKey
=
(
RSAPublicKey
)
KeyFactory
.
getInstance
(
"RSA"
).
generatePublic
(
new
X509EncodedKeySpec
(
decoded
));
//RSA加密
//RSA加密
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
pubKey
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
pubKey
);
String
outStr
=
Base64
.
encodeBase64
String
(
cipher
.
doFinal
(
str
.
getBytes
(
"UTF-8"
)));
String
outStr
=
Base64
Utils
.
encodeTo
String
(
cipher
.
doFinal
(
str
.
getBytes
(
"UTF-8"
)));
return
outStr
;
return
outStr
;
}
}
...
@@ -90,9 +93,9 @@ public class RSAUtils {
...
@@ -90,9 +93,9 @@ public class RSAUtils {
*/
*/
public
static
String
decrypt
(
String
str
,
String
privateKey
)
throws
Exception
{
public
static
String
decrypt
(
String
str
,
String
privateKey
)
throws
Exception
{
//64位解码加密后的字符串
//64位解码加密后的字符串
byte
[]
inputByte
=
Base64
.
decodeBase64
(
str
.
getBytes
(
"UTF-8"
)
);
byte
[]
inputByte
=
Base64
Utils
.
decodeFromString
(
str
);
//base64编码的私钥
//base64编码的私钥
byte
[]
decoded
=
Base64
.
decodeBase64
(
privateKey
);
byte
[]
decoded
=
Base64
Utils
.
decodeFromString
(
privateKey
);
RSAPrivateKey
priKey
=
(
RSAPrivateKey
)
KeyFactory
.
getInstance
(
"RSA"
).
generatePrivate
(
new
PKCS8EncodedKeySpec
(
decoded
));
RSAPrivateKey
priKey
=
(
RSAPrivateKey
)
KeyFactory
.
getInstance
(
"RSA"
).
generatePrivate
(
new
PKCS8EncodedKeySpec
(
decoded
));
//RSA解密
//RSA解密
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment