feat():测试

yuan
yuan 2024-09-05 22:31:45 +08:00
parent 955d634621
commit a71a996367
3 changed files with 97 additions and 29 deletions

View File

@ -21,13 +21,12 @@ import java.nio.file.Paths;
* @nameOSSFileCompile * @nameOSSFileCompile
* @Date2024/8/31 7:08 * @Date2024/8/31 7:08
*/ */
public class JavaFileCompile { public class FilesCompilerLoad {
private static final Logger log = LoggerFactory.getLogger(JavaFileCompile.class); private static final Logger log = LoggerFactory.getLogger(FilesCompilerLoad.class);
public static Result<Object> compile(String fileName){ public static Result<Object> javaCompiler(String fileName){
// try {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
String[] strings = {"-classpath","/home/lib","-verbose","-d", "/home/lib","home/"+fileName + MethodSuffix.JAVASUFFIX }; String[] strings = {"-classpath","/home/lib","-verbose","-d", "/home/lib","home/"+fileName + MethodSuffix.JAVASUFFIX };
@ -40,7 +39,6 @@ public class JavaFileCompile {
System.out.println("编译失败"); System.out.println("编译失败");
} }
try { try {
String classContent = ""; String classContent = "";
BufferedReader bufferedReader = new BufferedReader(new FileReader("/home/lib/com/muyu/generate/"+fileName+ MethodSuffix.CLASSSUFFIX)); BufferedReader bufferedReader = new BufferedReader(new FileReader("/home/lib/com/muyu/generate/"+fileName+ MethodSuffix.CLASSSUFFIX));
@ -57,28 +55,33 @@ public class JavaFileCompile {
e.printStackTrace(); e.printStackTrace();
} }
// // class文件路径 return Result.success();
// String externalClassFilePath = "/home/lib/com/muyu/generate/"+fileName+ MethodSuffix.CLASSSUFFIX; }
// log.info(externalClassFilePath);
// Path path = Paths.get(externalClassFilePath);
//
// String externalClassDir = externalClassFilePath.substring(0, externalClassFilePath.lastIndexOf("/"));
//
// URL[] urls = new URL[]{new File(externalClassDir).toURI().toURL()};
//
// ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls);
//
// // 加载类
// // 注意类名必须是完全限定名(包括包名)
// Class<?> clazz = externalClassLoader.loadClassFromPath(path, "/home/lib/com/muyu/generate/"+fileName+ MethodSuffix.CLASSSUFFIX);
// System.out.println(clazz);
//
// } catch (MalformedURLException e) {
// throw new RuntimeException(e);
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
public static Result<Object> classLoad(String fileName){
try {
// class文件路径
String externalClassFilePath = "/home/lib/com/muyu/generate/"+fileName+ MethodSuffix.CLASSSUFFIX;
log.info(externalClassFilePath);
Path path = Paths.get(externalClassFilePath);
String externalClassDir = externalClassFilePath.substring(0, externalClassFilePath.lastIndexOf("/"));
URL[] urls = new URL[]{new File(externalClassDir).toURI().toURL()};
ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls);
// 加载类
// 注意类名必须是完全限定名(包括包名)
Class<?> clazz = externalClassLoader.loadClassFromPath(path, "/home/lib/com/muyu/generate/"+fileName+ MethodSuffix.CLASSSUFFIX);
System.out.println(clazz);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return Result.success(); return Result.success();
} }

View File

@ -2,7 +2,7 @@ package com.muyu.load;
import com.aliyun.oss.OSSClient; import com.aliyun.oss.OSSClient;
import com.muyu.common.core.domain.Result; import com.muyu.common.core.domain.Result;
import com.muyu.compile.JavaFileCompile; import com.muyu.compile.FilesCompilerLoad;
import com.muyu.constant.MethodSuffix; import com.muyu.constant.MethodSuffix;
import java.io.*; import java.io.*;
@ -71,7 +71,69 @@ public class OSSFileLoad {
} }
//对路径里的.java文件进行编译 //对路径里的.java文件进行编译
System.out.println("执行编译"); System.out.println("执行编译");
JavaFileCompile.compile(fileName); FilesCompilerLoad.javaCompiler(fileName);
// SourceCodeCompiler.javaCompilerPath("home/");
// System.out.println("第二步");
// File outputDir = new File("home/"); // 或者是你指定的其他输出目录
// System.out.println("第三步");
// File[] classFiles = outputDir.listFiles(); // 获取输出目录中的所有文件
// if (classFiles != null) {
// for (File classFile : classFiles) {
// if (classFile.getName().endsWith(".class")) {
// System.out.println("找到class文件 " + classFile.getName());
//// 把.class文件存入oss中
// }
// }
// }else {
// System.out.println("没有找到文件");
// }
return Result.success(source);
}
public static Result<Object> streamingClassDownload(String fileName) throws IOException {
String source = null;
// 创建 OSSClient 实例
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 判断 bucket 是否存在
Boolean flag_bucket = ossClient.doesBucketExist(bucketName);
if (!flag_bucket) {
System.out.println("bucket不存在");
return Result.error("bucket不存在");
}
Boolean flag_file = ossClient.doesObjectExist(bucketName, fileJavaPath+fileName+ MethodSuffix.CLASSSUFFIX);
if (!flag_file) {
System.out.println("预下载文件不存在");
return Result.error("预下载文件不存在");
}
// 本地文件下载路径
String localPath = "home/class/" + fileName + MethodSuffix.CLASSSUFFIX;
try {
// 从OSS下载文件
InputStream inputStream = ossClient.getObject(bucketName, fileJavaPath + fileName + MethodSuffix.CLASSSUFFIX).getObjectContent();
File downloadFile = new File(localPath);
FileOutputStream outputStream = new FileOutputStream(downloadFile);
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.close();
inputStream.close();
System.out.println("存放路径:" + localPath);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭OSSClient
if (ossClient != null) {
ossClient.shutdown();
}
}
//对路径里的.java文件进行编译
System.out.println("执行编译");
FilesCompilerLoad.classLoad(fileName);
// SourceCodeCompiler.javaCompilerPath("home/"); // SourceCodeCompiler.javaCompilerPath("home/");
// System.out.println("第二步"); // System.out.println("第二步");
// File outputDir = new File("home/"); // 或者是你指定的其他输出目录 // File outputDir = new File("home/"); // 或者是你指定的其他输出目录

View File

@ -4,7 +4,9 @@ import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest; import com.aliyun.oss.model.PutObjectRequest;
import com.muyu.common.core.domain.Result; import com.muyu.common.core.domain.Result;
import com.muyu.compile.FilesCompilerLoad;
import com.muyu.constant.MethodSuffix; import com.muyu.constant.MethodSuffix;
import com.muyu.load.OSSFileLoad;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -84,11 +86,12 @@ public class ALiYunUpload {
// 执行上传操作 // 执行上传操作
ossClient.putObject(putObjectRequest); ossClient.putObject(putObjectRequest);
OSSFileLoad.streamingClassDownload(fileName);
return Result.success(null,"文件"+fileName+".class"+"存储成功"); return Result.success(null,"文件"+fileName+".class"+"存储成功");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return Result.error(null,"文件"+fileName+".class"+"存储失败"); return Result.error(null,"文件"+fileName+".class"+"存储失败");
} finally { } finally {
// 关闭OSSClient // 关闭OSSClient