Commit 0be2d805 by tyfanchz

增加文件上传类

1 parent 2931efd4
......@@ -60,6 +60,23 @@
<artifactId>spring-data-commons</artifactId>
<version>2.4.2</version>
</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>
<build>
......@@ -77,4 +94,4 @@
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
</project>
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;
}
}
......@@ -15,6 +15,8 @@ public enum ResultCodeEnum {
REQUEST_ERROR(401, "请求错误"),
PARAM_ERROR(402, "参数错误"),
SERVER_ERROR(500, "系统异常"),
FILE_UPLOAD_ERROR(501, "文件上传失败"),
FILE_DELETE_ERROR(502, "文件删除失败"),
;
private final Integer code;
......
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;
}
}
package cn.com.glsx.openplatform.util;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;
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.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
/**
......@@ -20,7 +23,7 @@ import java.util.Map;
*/
@Component
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 {
//生成公钥和私钥
......@@ -52,9 +55,9 @@ public class RSAUtils {
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到私钥
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
keyMap.put(0, publicKeyString); //0表示公钥
keyMap.put(1, privateKeyString); //1表示私钥
......@@ -71,12 +74,12 @@ public class RSAUtils {
*/
public static String encrypt(String str, String publicKey) throws Exception {
//base64编码的公钥
byte[] decoded = Base64.decodeBase64(publicKey);
byte[] decoded = Base64Utils.decodeFromString(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
String outStr = Base64Utils.encodeToString(cipher.doFinal(str.getBytes("UTF-8")));
return outStr;
}
......@@ -90,9 +93,9 @@ public class RSAUtils {
*/
public static String decrypt(String str, String privateKey) throws Exception {
//64位解码加密后的字符串
byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
byte[] inputByte = Base64Utils.decodeFromString(str);
//base64编码的私钥
byte[] decoded = Base64.decodeBase64(privateKey);
byte[] decoded = Base64Utils.decodeFromString(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance("RSA");
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!