--9--10
parent
a2c09229e8
commit
6e53a7cfa9
|
@ -159,9 +159,11 @@ public class EngineVersionController extends BaseController {
|
|||
@PostMapping("/insertVersion")
|
||||
public boolean insertVersion(@RequestBody EngineVersion engineVersion) {
|
||||
|
||||
// OssUpload.uploadFiles(engineVersion.getRuleContent(), engineVersion.getVersionCode());
|
||||
//
|
||||
// JavaClass.compile(engineVersion.getName());
|
||||
OssUpload.uploadFiles(engineVersion.getRuleContent(), engineVersion.getVersionCode());
|
||||
|
||||
OSSFileDownload.streamingDownload(engineVersion.getName());
|
||||
|
||||
JavaClass.compile(engineVersion.getName());
|
||||
|
||||
engineVersion.setId(null);
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ import javax.tools.ToolProvider;
|
|||
public class JavaClass {
|
||||
public static Result<Object> compile(String fileName) {
|
||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
String[] strings = {"-classpath", "/home/lib/com/muyu/abstracts/build/rule/version/", "-verbose", "-d", "/home/lib", "/home/" + fileName + ".java"};
|
||||
String[] strings = {"-classpath", "/home/lib", "-verbose", "-d", "/home/lib", "/home/" + fileName + ".java"};
|
||||
int result = compiler.run(null, null, null, strings);
|
||||
if (result == 0) {
|
||||
log.info("编译成功,生成的.class文件位于源代码同目录" + fileName + ".class");
|
||||
|
|
|
@ -1,77 +1,77 @@
|
|||
package com.muyu.utils;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.model.GetObjectRequest;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
import com.aliyun.oss.model.ObjectListing;
|
||||
import com.aliyun.oss.model.OSSObjectSummary;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
@Log4j2
|
||||
@Component
|
||||
public class DownloadOssSynchronization implements ApplicationRunner {
|
||||
|
||||
private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
||||
|
||||
private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ";
|
||||
|
||||
private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x";
|
||||
|
||||
private static final String bucketName = "011811";
|
||||
|
||||
/**
|
||||
* 服务器项目路径放文件
|
||||
*/
|
||||
private static String serverClassPath = "/home/lib/com/muyu/abstracts/";
|
||||
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
log.info("开始从Oss下载规则引擎");
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
|
||||
// 获取存储桶信息
|
||||
ObjectListing objectListing = ossClient.listObjects(bucketName);
|
||||
// 循环获取Oss存储桶中的所有对象
|
||||
for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
|
||||
String key = objectSummary.getKey();
|
||||
downloadFile(ossClient, bucketName, key, serverClassPath);
|
||||
}
|
||||
// 关闭OSSClient
|
||||
ossClient.shutdown();
|
||||
//下载完成实现批量批量类加载
|
||||
}
|
||||
|
||||
private void downloadFile(OSS ossClient, String bucketName, String key, String serverPath) {
|
||||
log.info("开始下载文件[]{}:"+key);
|
||||
//创建服务器项目容器的文件目录路径
|
||||
File serverFile = new File(serverPath, key);
|
||||
//如果目录不存在,则创建目录
|
||||
if (!serverFile.getParentFile().exists()) {
|
||||
serverFile.getParentFile().mkdirs();
|
||||
}
|
||||
//下载文件到项目中
|
||||
OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, key));
|
||||
|
||||
try (FileOutputStream fos = new FileOutputStream(serverFile);) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int byteRead;
|
||||
while ((byteRead = ossObject.getObjectContent().read(buffer)) != -1) {
|
||||
fos.write(buffer, 0, byteRead);
|
||||
}
|
||||
log.info("文件 " + key + "已经下载到" + serverFile.getAbsolutePath());
|
||||
} catch (IOException e) {
|
||||
log.error("下载文件失败:" + key);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//package com.muyu.utils;
|
||||
//
|
||||
//import com.aliyun.oss.OSS;
|
||||
//import com.aliyun.oss.OSSClientBuilder;
|
||||
//import com.aliyun.oss.model.GetObjectRequest;
|
||||
//import com.aliyun.oss.model.OSSObject;
|
||||
//import com.aliyun.oss.model.ObjectListing;
|
||||
//import com.aliyun.oss.model.OSSObjectSummary;
|
||||
//import lombok.extern.log4j.Log4j2;
|
||||
//import org.springframework.boot.ApplicationArguments;
|
||||
//import org.springframework.boot.ApplicationRunner;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//import java.io.File;
|
||||
//import java.io.FileOutputStream;
|
||||
//import java.io.IOException;
|
||||
//
|
||||
//@Log4j2
|
||||
//@Component
|
||||
//public class DownloadOssSynchronization implements ApplicationRunner {
|
||||
//
|
||||
// private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
||||
//
|
||||
// private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ";
|
||||
//
|
||||
// private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x";
|
||||
//
|
||||
// private static final String bucketName = "011811";
|
||||
//
|
||||
// /**
|
||||
// * 服务器项目路径放文件
|
||||
// */
|
||||
// private static String serverClassPath = "/home/lib/com/muyu/abstracts/";
|
||||
//
|
||||
//
|
||||
// @Override
|
||||
// public void run(ApplicationArguments args) throws Exception {
|
||||
// log.info("开始从Oss下载规则引擎");
|
||||
// OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
//
|
||||
// // 获取存储桶信息
|
||||
// ObjectListing objectListing = ossClient.listObjects(bucketName);
|
||||
// // 循环获取Oss存储桶中的所有对象
|
||||
// for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
|
||||
// String key = objectSummary.getKey();
|
||||
// downloadFile(ossClient, bucketName, key, serverClassPath);
|
||||
// }
|
||||
// // 关闭OSSClient
|
||||
// ossClient.shutdown();
|
||||
// //下载完成实现批量批量类加载
|
||||
// }
|
||||
//
|
||||
// private void downloadFile(OSS ossClient, String bucketName, String key, String serverPath) {
|
||||
// log.info("开始下载文件[]{}:"+key);
|
||||
// //创建服务器项目容器的文件目录路径
|
||||
// File serverFile = new File(serverPath, key);
|
||||
// //如果目录不存在,则创建目录
|
||||
// if (!serverFile.getParentFile().exists()) {
|
||||
// serverFile.getParentFile().mkdirs();
|
||||
// }
|
||||
// //下载文件到项目中
|
||||
// OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, key));
|
||||
//
|
||||
// try (FileOutputStream fos = new FileOutputStream(serverFile);) {
|
||||
// byte[] buffer = new byte[1024];
|
||||
// int byteRead;
|
||||
// while ((byteRead = ossObject.getObjectContent().read(buffer)) != -1) {
|
||||
// fos.write(buffer, 0, byteRead);
|
||||
// }
|
||||
// log.info("文件 " + key + "已经下载到" + serverFile.getAbsolutePath());
|
||||
// } catch (IOException e) {
|
||||
// log.error("下载文件失败:" + key);
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
|
|
@ -1,91 +1,91 @@
|
|||
package com.muyu.utils;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* @Author:LiDongJia
|
||||
* @Package:com.muyu.cloud.etl.rule.upload
|
||||
* @Project:cloud-rule
|
||||
* @name:ALiYunUpload
|
||||
* @Date:2024/8/30
|
||||
*/
|
||||
public class OssUpload {
|
||||
|
||||
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写
|
||||
private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
||||
|
||||
private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ";
|
||||
|
||||
private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x";
|
||||
|
||||
private static final String bucketName = "011811";
|
||||
// Bucket名称
|
||||
|
||||
public static Result<Object> uploadFiles(String content, String fileName) {
|
||||
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
|
||||
// 上传到OSS后文件保存的目录,例如:folder/subfolder/,最后以斜杠结尾
|
||||
String objectName = "build/rule/version/" + fileName + ".java";
|
||||
|
||||
try {
|
||||
// 将内容转换为字节数组输入流
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
// 构造上传请求
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
|
||||
|
||||
// 执行上传操作
|
||||
// ossClient.putObject(putObjectRequest);
|
||||
|
||||
return Result.success(null, "文件" + fileName + ".java" + "存储成功");
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
return Result.error(null, "文件" + fileName + ".java" + "存储失败");
|
||||
} finally {
|
||||
// 关闭OSSClient
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Result<Object> uploadClassFiles(String versionClass) {
|
||||
// 创建OSSClient实例。
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
|
||||
try {
|
||||
// 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
|
||||
String objectName = "build/rule/version/" + versionClass + ".class";
|
||||
|
||||
// 创建上传文件的输入流(这里以文件为例)。
|
||||
File file = new File("/home/lib/com/muyu/cloud/rule/engine/" + versionClass + ".class");
|
||||
|
||||
// 上传文件。
|
||||
ossClient.putObject(new PutObjectRequest(bucketName, objectName, file));
|
||||
|
||||
System.out.println("上传成功");
|
||||
return Result.success("上传成功");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("上传失败");
|
||||
return Result.error("上传失败");
|
||||
} finally {
|
||||
// 关闭OSSClient。
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//package com.muyu.utils;
|
||||
//
|
||||
//import com.aliyun.oss.OSS;
|
||||
//import com.aliyun.oss.OSSClientBuilder;
|
||||
//import com.aliyun.oss.model.PutObjectRequest;
|
||||
//import com.muyu.common.core.domain.Result;
|
||||
//
|
||||
//import java.io.ByteArrayInputStream;
|
||||
//import java.io.File;
|
||||
//import java.nio.charset.StandardCharsets;
|
||||
//
|
||||
///**
|
||||
// * @Author:LiDongJia
|
||||
// * @Package:com.muyu.cloud.etl.rule.upload
|
||||
// * @Project:cloud-rule
|
||||
// * @name:ALiYunUpload
|
||||
// * @Date:2024/8/30
|
||||
// */
|
||||
//public class OssUpload {
|
||||
//
|
||||
// // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写
|
||||
// private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
||||
//
|
||||
// private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ";
|
||||
//
|
||||
// private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x";
|
||||
//
|
||||
// private static final String bucketName = "011811";
|
||||
// // Bucket名称
|
||||
//
|
||||
// public static Result<Object> uploadFiles(String content, String fileName) {
|
||||
//
|
||||
// OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
//
|
||||
// // 上传到OSS后文件保存的目录,例如:folder/subfolder/,最后以斜杠结尾
|
||||
// String objectName = "build/rule/version/" + fileName + ".java";
|
||||
//
|
||||
// try {
|
||||
// // 将内容转换为字节数组输入流
|
||||
// ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
|
||||
//
|
||||
// // 构造上传请求
|
||||
// PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
|
||||
//
|
||||
// // 执行上传操作
|
||||
//// ossClient.putObject(putObjectRequest);
|
||||
//
|
||||
// return Result.success(null, "文件" + fileName + ".java" + "存储成功");
|
||||
//
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
//
|
||||
// return Result.error(null, "文件" + fileName + ".java" + "存储失败");
|
||||
// } finally {
|
||||
// // 关闭OSSClient
|
||||
// if (ossClient != null) {
|
||||
// ossClient.shutdown();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public static Result<Object> uploadClassFiles(String versionClass) {
|
||||
// // 创建OSSClient实例。
|
||||
// OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
//
|
||||
// try {
|
||||
// // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
|
||||
// String objectName = "build/rule/version/" + versionClass + ".class";
|
||||
//
|
||||
// // 创建上传文件的输入流(这里以文件为例)。
|
||||
// File file = new File("/home/lib/com/muyu/cloud/rule/engine/" + versionClass + ".class");
|
||||
//
|
||||
// // 上传文件。
|
||||
// ossClient.putObject(new PutObjectRequest(bucketName, objectName, file));
|
||||
//
|
||||
// System.out.println("上传成功");
|
||||
// return Result.success("上传成功");
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// System.out.println("上传失败");
|
||||
// return Result.error("上传失败");
|
||||
// } finally {
|
||||
// // 关闭OSSClient。
|
||||
// if (ossClient != null) {
|
||||
// ossClient.shutdown();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
|
Loading…
Reference in New Issue