增加阿里云oss的图片存储方式
This commit is contained in:
parent
89642e680f
commit
b30640c940
@ -719,6 +719,12 @@ INSERT INTO `jsh_platform_config` VALUES ('9', 'send_workflow_url', '发起流
|
||||
INSERT INTO `jsh_platform_config` VALUES ('10', 'weixinUrl', '微信url', '');
|
||||
INSERT INTO `jsh_platform_config` VALUES ('11', 'weixinAppid', '微信appid', '');
|
||||
INSERT INTO `jsh_platform_config` VALUES ('12', 'weixinSecret', '微信secret', '');
|
||||
INSERT INTO `jsh_platform_config` VALUES ('13', 'aliOss_endpoint', '阿里OSS-endpoint', '');
|
||||
INSERT INTO `jsh_platform_config` VALUES ('14', 'aliOss_accessKeyId', '阿里OSS-accessKeyId', '');
|
||||
INSERT INTO `jsh_platform_config` VALUES ('15', 'aliOss_accessKeySecret', '阿里OSS-accessKeySecret', '');
|
||||
INSERT INTO `jsh_platform_config` VALUES ('16', 'aliOss_bucketName', '阿里OSS-bucketName', '');
|
||||
INSERT INTO `jsh_platform_config` VALUES ('17', 'aliOss_linkUrl', '阿里OSS-linkUrl', '');
|
||||
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for jsh_role
|
||||
|
||||
@ -1498,4 +1498,15 @@ alter table jsh_material add position varchar(100) DEFAULT NULL COMMENT '仓位
|
||||
alter table jsh_user add weixin_open_id varchar(100) DEFAULT NULL COMMENT '微信绑定' after remark;
|
||||
INSERT INTO `jsh_platform_config` (`platform_key`, `platform_key_info`, `platform_value`) VALUES ('weixinUrl', '微信url', '');
|
||||
INSERT INTO `jsh_platform_config` (`platform_key`, `platform_key_info`, `platform_value`) VALUES ('weixinAppid', '微信appid', '');
|
||||
INSERT INTO `jsh_platform_config` (`platform_key`, `platform_key_info`, `platform_value`) VALUES ('weixinSecret', '微信secret', '');
|
||||
INSERT INTO `jsh_platform_config` (`platform_key`, `platform_key_info`, `platform_value`) VALUES ('weixinSecret', '微信secret', '');
|
||||
|
||||
-- --------------------------------------------------------
|
||||
-- 时间 2023年06月05日
|
||||
-- by jishenghua
|
||||
-- 给平台表增加阿里云OSS的相关信息
|
||||
-- --------------------------------------------------------
|
||||
INSERT INTO `jsh_platform_config` (`platform_key`, `platform_key_info`, `platform_value`) VALUES ('aliOss_endpoint', '阿里OSS-endpoint', '');
|
||||
INSERT INTO `jsh_platform_config` (`platform_key`, `platform_key_info`, `platform_value`) VALUES ('aliOss_accessKeyId', '阿里OSS-accessKeyId', '');
|
||||
INSERT INTO `jsh_platform_config` (`platform_key`, `platform_key_info`, `platform_value`) VALUES ('aliOss_accessKeySecret', '阿里OSS-accessKeySecret', '');
|
||||
INSERT INTO `jsh_platform_config` (`platform_key`, `platform_key_info`, `platform_value`) VALUES ('aliOss_bucketName', '阿里OSS-bucketName', '');
|
||||
INSERT INTO `jsh_platform_config` (`platform_key`, `platform_key_info`, `platform_value`) VALUES ('aliOss_linkUrl', '阿里OSS-linkUrl', '');
|
||||
@ -103,6 +103,11 @@
|
||||
<artifactId>swagger-bootstrap-ui</artifactId>
|
||||
<version>1.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>3.10.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@ -28,6 +28,8 @@ import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -53,6 +55,9 @@ public class SystemConfigController {
|
||||
@Resource
|
||||
private SystemConfigService systemConfigService;
|
||||
|
||||
@Value(value="${file.uploadType}")
|
||||
private Long fileUploadType;
|
||||
|
||||
@Value(value="${file.path}")
|
||||
private String filePath;
|
||||
|
||||
@ -128,13 +133,11 @@ public class SystemConfigController {
|
||||
String name = request.getParameter("name");
|
||||
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
||||
MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象
|
||||
if(StringUtil.isEmpty(bizPath)){
|
||||
bizPath = "";
|
||||
if(fileUploadType == 1) {
|
||||
savePath = systemConfigService.uploadLocal(file, bizPath, name, request);
|
||||
} else if(fileUploadType == 2) {
|
||||
savePath = systemConfigService.uploadAliOss(file, bizPath, name, request);
|
||||
}
|
||||
String token = request.getHeader("X-Access-Token");
|
||||
Long tenantId = Tools.getTenantIdByToken(token);
|
||||
bizPath = bizPath + File.separator + tenantId;
|
||||
savePath = this.uploadLocal(file, bizPath, name);
|
||||
if(StringUtil.isNotEmpty(savePath)){
|
||||
res.code = 200;
|
||||
res.data = savePath;
|
||||
@ -150,50 +153,6 @@ public class SystemConfigController {
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 本地文件上传
|
||||
* @param mf 文件
|
||||
* @param bizPath 自定义路径
|
||||
* @return
|
||||
*/
|
||||
private String uploadLocal(MultipartFile mf,String bizPath,String name){
|
||||
try {
|
||||
String ctxPath = filePath;
|
||||
String fileName = null;
|
||||
File file = new File(ctxPath + File.separator + bizPath + File.separator );
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();// 创建文件根目录
|
||||
}
|
||||
String orgName = mf.getOriginalFilename();// 获取文件名
|
||||
orgName = FileUtils.getFileName(orgName);
|
||||
if(orgName.indexOf(".")!=-1){
|
||||
if(StringUtil.isNotEmpty(name)) {
|
||||
fileName = name.substring(0, name.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
|
||||
} else {
|
||||
fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
|
||||
}
|
||||
}else{
|
||||
fileName = orgName+ "_" + System.currentTimeMillis();
|
||||
}
|
||||
String savePath = file.getPath() + File.separator + fileName;
|
||||
File savefile = new File(savePath);
|
||||
FileCopyUtils.copy(mf.getBytes(), savefile);
|
||||
String dbpath = null;
|
||||
if(StringUtil.isNotEmpty(bizPath)){
|
||||
dbpath = bizPath + File.separator + fileName;
|
||||
}else{
|
||||
dbpath = fileName;
|
||||
}
|
||||
if (dbpath.contains("\\")) {
|
||||
dbpath = dbpath.replace("\\", "/");
|
||||
}
|
||||
return dbpath;
|
||||
} catch (IOException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 预览图片&下载文件
|
||||
* 请求地址:http://localhost:8080/common/static/{financial/afsdfasdfasdf_1547866868179.txt}
|
||||
@ -217,15 +176,25 @@ public class SystemConfigController {
|
||||
if (imgPath.endsWith(",")) {
|
||||
imgPath = imgPath.substring(0, imgPath.length() - 1);
|
||||
}
|
||||
String fileUrl = filePath + File.separator + imgPath;
|
||||
File file = new File(fileUrl);
|
||||
if(!file.exists()){
|
||||
response.setStatus(404);
|
||||
throw new RuntimeException("文件不存在..");
|
||||
String fileUrl = "";
|
||||
if(fileUploadType == 1) {
|
||||
fileUrl = systemConfigService.getFileUrlLocal(imgPath);
|
||||
File file = new File(fileUrl);
|
||||
if(!file.exists()){
|
||||
response.setStatus(404);
|
||||
throw new RuntimeException("文件不存在..");
|
||||
}
|
||||
response.setContentType("application/force-download");// 设置强制下载不打开
|
||||
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"),"iso-8859-1"));
|
||||
inputStream = new BufferedInputStream(new FileInputStream(fileUrl));
|
||||
} else if(fileUploadType == 2) {
|
||||
fileUrl = systemConfigService.getFileUrlAliOss(imgPath);
|
||||
URL url = new URL(fileUrl);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setConnectTimeout(5 * 1000);
|
||||
inputStream = conn.getInputStream();// 通过输入流获取图片数据
|
||||
}
|
||||
response.setContentType("application/force-download");// 设置强制下载不打开
|
||||
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"),"iso-8859-1"));
|
||||
inputStream = new BufferedInputStream(new FileInputStream(fileUrl));
|
||||
outputStream = response.getOutputStream();
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
@ -237,6 +206,9 @@ public class SystemConfigController {
|
||||
logger.error("预览文件失败" + e.getMessage());
|
||||
response.setStatus(404);
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
response.setStatus(404);
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
|
||||
@ -1,6 +1,12 @@
|
||||
package com.jsh.erp.service.systemConfig;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyun.oss.ClientException;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.OSSException;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.aliyun.oss.model.PutObjectResult;
|
||||
import com.jsh.erp.constants.BusinessConstants;
|
||||
import com.jsh.erp.datasource.entities.SystemConfig;
|
||||
import com.jsh.erp.datasource.entities.SystemConfigExample;
|
||||
@ -9,16 +15,24 @@ import com.jsh.erp.datasource.mappers.SystemConfigMapper;
|
||||
import com.jsh.erp.datasource.mappers.SystemConfigMapperEx;
|
||||
import com.jsh.erp.exception.JshException;
|
||||
import com.jsh.erp.service.log.LogService;
|
||||
import com.jsh.erp.service.platformConfig.PlatformConfigService;
|
||||
import com.jsh.erp.service.user.UserService;
|
||||
import com.jsh.erp.utils.FileUtils;
|
||||
import com.jsh.erp.utils.StringUtil;
|
||||
import com.jsh.erp.utils.Tools;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.*;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@ -28,14 +42,18 @@ public class SystemConfigService {
|
||||
|
||||
@Resource
|
||||
private SystemConfigMapper systemConfigMapper;
|
||||
|
||||
@Resource
|
||||
private SystemConfigMapperEx systemConfigMapperEx;
|
||||
@Resource
|
||||
private PlatformConfigService platformConfigService;
|
||||
@Resource
|
||||
private UserService userService;
|
||||
@Resource
|
||||
private LogService logService;
|
||||
|
||||
@Value(value="${file.path}")
|
||||
private String filePath;
|
||||
|
||||
public SystemConfig getSystemConfig(long id)throws Exception {
|
||||
SystemConfig result=null;
|
||||
try{
|
||||
@ -143,6 +161,132 @@ public class SystemConfigService {
|
||||
return list==null?0:list.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* 本地文件上传
|
||||
* @param mf 文件
|
||||
* @param bizPath 自定义路径
|
||||
* @param name 自定义文件名
|
||||
* @return
|
||||
*/
|
||||
public String uploadLocal(MultipartFile mf, String bizPath, String name, HttpServletRequest request) {
|
||||
try {
|
||||
if(StringUtil.isEmpty(bizPath)){
|
||||
bizPath = "";
|
||||
}
|
||||
String token = request.getHeader("X-Access-Token");
|
||||
Long tenantId = Tools.getTenantIdByToken(token);
|
||||
bizPath = bizPath + File.separator + tenantId;
|
||||
String ctxPath = filePath;
|
||||
String fileName = null;
|
||||
File file = new File(ctxPath + File.separator + bizPath + File.separator );
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();// 创建文件根目录
|
||||
}
|
||||
String orgName = mf.getOriginalFilename();// 获取文件名
|
||||
orgName = FileUtils.getFileName(orgName);
|
||||
if(orgName.contains(".")){
|
||||
if(StringUtil.isNotEmpty(name)) {
|
||||
fileName = name.substring(0, name.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
|
||||
} else {
|
||||
fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
|
||||
}
|
||||
}else{
|
||||
fileName = orgName+ "_" + System.currentTimeMillis();
|
||||
}
|
||||
String savePath = file.getPath() + File.separator + fileName;
|
||||
File savefile = new File(savePath);
|
||||
FileCopyUtils.copy(mf.getBytes(), savefile);
|
||||
String dbpath = null;
|
||||
if(StringUtil.isNotEmpty(bizPath)){
|
||||
dbpath = bizPath + File.separator + fileName;
|
||||
}else{
|
||||
dbpath = fileName;
|
||||
}
|
||||
if (dbpath.contains("\\")) {
|
||||
dbpath = dbpath.replace("\\", "/");
|
||||
}
|
||||
return dbpath;
|
||||
} catch (IOException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 阿里Oss文件上传
|
||||
* @param mf 文件
|
||||
* @param bizPath 自定义路径
|
||||
* @param name 自定义文件名
|
||||
* @return
|
||||
*/
|
||||
public String uploadAliOss(MultipartFile mf, String bizPath, String name, HttpServletRequest request) throws Exception {
|
||||
if(StringUtil.isEmpty(bizPath)){
|
||||
bizPath = "";
|
||||
}
|
||||
String token = request.getHeader("X-Access-Token");
|
||||
Long tenantId = Tools.getTenantIdByToken(token);
|
||||
bizPath = bizPath + "/" + tenantId;
|
||||
String endpoint = platformConfigService.getPlatformConfigByKey("aliOss_endpoint").getPlatformValue();
|
||||
String accessKeyId = platformConfigService.getPlatformConfigByKey("aliOss_accessKeyId").getPlatformValue();
|
||||
String accessKeySecret = platformConfigService.getPlatformConfigByKey("aliOss_accessKeySecret").getPlatformValue();
|
||||
String bucketName = platformConfigService.getPlatformConfigByKey("aliOss_bucketName").getPlatformValue();
|
||||
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
|
||||
String fileName = "";
|
||||
String orgName = mf.getOriginalFilename();// 获取文件名
|
||||
orgName = FileUtils.getFileName(orgName);
|
||||
if(orgName.contains(".")){
|
||||
if(StringUtil.isNotEmpty(name)) {
|
||||
fileName = name.substring(0, name.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
|
||||
} else {
|
||||
fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
|
||||
}
|
||||
}else{
|
||||
fileName = orgName+ "_" + System.currentTimeMillis();
|
||||
}
|
||||
String filePathStr = StringUtil.isNotEmpty(filePath)? filePath.substring(1):"";
|
||||
String objectName = filePathStr + "/" + bizPath + "/" + fileName;
|
||||
// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
|
||||
byte [] byteArr = mf.getBytes();
|
||||
|
||||
// 创建OSSClient实例。
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
|
||||
try {
|
||||
InputStream inputStream = new ByteArrayInputStream(byteArr);
|
||||
// 创建PutObjectRequest对象。
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
|
||||
// 创建PutObject请求。
|
||||
PutObjectResult result = ossClient.putObject(putObjectRequest);
|
||||
return bizPath + "/" + fileName;
|
||||
} catch (OSSException oe) {
|
||||
logger.error("Caught an OSSException, which means your request made it to OSS, "
|
||||
+ "but was rejected with an error response for some reason.");
|
||||
logger.error("Error Message:" + oe.getErrorMessage());
|
||||
logger.error("Error Code:" + oe.getErrorCode());
|
||||
logger.error("Request ID:" + oe.getRequestId());
|
||||
logger.error("Host ID:" + oe.getHostId());
|
||||
} catch (ClientException ce) {
|
||||
logger.error("Caught an ClientException, which means the client encountered "
|
||||
+ "a serious internal problem while trying to communicate with OSS, "
|
||||
+ "such as not being able to access the network.");
|
||||
System.out.println("Error Message:" + ce.getMessage());
|
||||
} finally {
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public String getFileUrlLocal(String imgPath) {
|
||||
return filePath + File.separator + imgPath;
|
||||
}
|
||||
|
||||
public String getFileUrlAliOss(String imgPath) throws Exception {
|
||||
String linkUrl = platformConfigService.getPlatformConfigByKey("aliOss_linkUrl").getPlatformValue();
|
||||
return linkUrl + filePath + "/" + imgPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取仓库开关
|
||||
* @return
|
||||
@ -261,4 +405,4 @@ public class SystemConfigService {
|
||||
}
|
||||
return multiLevelApprovalFlag;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -24,6 +24,8 @@ tenant.tryDayLimit=3000
|
||||
plugin.runMode=prod
|
||||
plugin.pluginPath=plugins
|
||||
plugin.pluginConfigFilePath=pluginConfig
|
||||
#文件上传方式 1-本机 2-oss
|
||||
file.uploadType=1
|
||||
#文件上传根目录
|
||||
file.path=/opt/jshERP/upload
|
||||
#文件上传临时路径
|
||||
|
||||
Loading…
Reference in New Issue
Block a user