Commit 5c58920d authored by panyf's avatar panyf
Browse files

[CPDemo]修改代理配置,增加dockerfile文件,实现服务器端的部署,完成使用的基本要求。

parent 99c05d90
# 使用官方的 OpenJDK 基础镜像
FROM openjdk:17-jdk-slim
RUN apt-get update && apt-get install -y \
cmake \
g++ \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# 设置工作目录
WORKDIR /app
......
......@@ -7,32 +7,63 @@
:auto-upload="false"
:on-change="handleFileChange"
:file-list="fileList"
accept=".cs"
accept=".cc"
:multiple="false"
>
<template #trigger>
<el-button size="small" type="primary">选取文件</el-button>
</template>
<el-button style="margin-left: 10px;" size="small" type="success" @click="uploadFile">上传并发布</el-button>
<el-button
style="margin-left: 10px;"
size="small"
type="success"
:disabled="isUploading"
@click="uploadFile"
>
上传并编译
</el-button>
</el-upload>
<el-link
:href="downloadUrl"
v-if="downloadUrl"
:download="downloadFileName"
:disabled="!downloadUrl"
<!-- 动态进度条 -->
<el-progress
v-if="isUploading"
:percentage="progressPercentage"
:status="progressStatus"
class="mt-4"
>
下载发布后的文件
</el-link>
:stroke-width="10"
:show-info="false"
></el-progress>
<!-- 成功提示 -->
<el-alert
v-if="isSuccess"
title="编译成功!"
type="success"
show-icon
class="mt-4"
></el-alert>
<!-- 错误信息栏 -->
<el-alert
v-if="showErrorMessage"
title="发布错误信息"
type="error"
:visible="errorMessage!== ''"
:closable="false"
class="mt-4"
>
{{ errorMessage }}
</el-alert>
<!-- 下载按钮 -->
<el-button
v-if="isDownloadAvailable"
size="small"
type="primary"
@click="downloadFile"
class="mt-4"
>
下载标定程序
</el-button>
</div>
</div>
</template>
......@@ -45,8 +76,15 @@ export default {
fileList: [],
selectedFile: null,
downloadUrl: null,
downloadFileName: 'CxMesUploader.exe',
errorMessage: ''
downloadFileName: 'radarcal',
errorMessage: '',
showErrorMessage: false, // 控制错误信息栏的显示
isUploading: false, // 是否正在上传和编译
progressPercentage: 0, // 进度条百分比
progressStatus: '', // 进度条状态(success/error)
isSuccess: false, // 是否编译成功
isDownloadAvailable: false, // 是否显示下载按钮
progressInterval: null // 用于存储进度条更新的定时器
};
},
methods: {
......@@ -59,27 +97,69 @@ export default {
this.$message.error('请选择一个文件');
return;
}
const formData = new FormData();
formData.append('file', this.selectedFile);
// 清空错误信息和成功提示
this.errorMessage = '';
this.showErrorMessage = false;
this.isSuccess = false;
// 开始上传和编译
this.isUploading = true;
this.progressPercentage = 0;
this.progressStatus = '';
// 设置进度条定时器,100 秒内均匀增长到 100%
this.progressInterval = setInterval(() => {
if (this.progressPercentage < 100) {
this.progressPercentage += 1; // 每秒增加 1%
} else {
clearInterval(this.progressInterval); // 停止定时器
}
}, 1000); // 每秒更新一次进度条
try {
const formData = new FormData();
formData.append('file', this.selectedFile);
const response = await fetch(this.uploadUrl, {
method: 'POST',
body: formData
});
clearInterval(this.progressInterval); // 停止进度条定时器
if (response.ok) {
const blob = await response.blob();
this.downloadUrl = window.URL.createObjectURL(blob);
this.errorMessage = '';
this.isDownloadAvailable = true; // 显示下载按钮
this.isSuccess = true; // 标记编译成功
this.progressPercentage = 100; // 进度条完成
this.progressStatus = 'success'; // 进度条状态为成功
this.$message.success('编译成功!'); // 提示编译成功
} else {
this.errorMessage = await response.text();
this.$message.error('上传或发布失败');
this.downloadUrl = null;
const errorText = await response.text();
this.errorMessage = errorText;
this.showErrorMessage = true; // 显示错误信息栏
this.progressStatus = 'error'; // 进度条状态为失败
}
} catch (error) {
clearInterval(this.progressInterval); // 停止进度条定时器
console.error('发生错误:', error);
this.errorMessage = '发生错误,请重试';
this.$message.error('发生错误,请重试');
this.downloadUrl = null;
this.showErrorMessage = true; // 显示错误信息栏
this.progressStatus = 'error'; // 进度条状态为失败
} finally {
this.isUploading = false; // 隐藏进度条
}
},
downloadFile() {
if (this.downloadUrl) {
const link = document.createElement('a');
link.href = this.downloadUrl;
link.download = this.downloadFileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
}
......
......@@ -23,8 +23,8 @@ import java.util.stream.Collectors;
@RestController
public class FileUploadController {
private static final String UPLOAD_DIR = "/home/blapapple/temp/devicemanager/programs";
private static final String TARGET_FILE = "Program.cs";
private static final String UPLOAD_DIR = "/home/devicemanager/programs";
private static final String TARGET_FILE = "radarcal.cc";
@PostMapping("/upload")
public ResponseEntity<Object> uploadFile(@RequestParam("file") MultipartFile file) {
......@@ -35,6 +35,7 @@ public class FileUploadController {
uploadDir.mkdirs();
}
System.out.println("upload file: " + file.getOriginalFilename());
// 保存上传的文件
Path targetFilePath = Paths.get(UPLOAD_DIR, TARGET_FILE);
File tempFile = File.createTempFile("temp-", ".cs");
......@@ -44,16 +45,16 @@ public class FileUploadController {
Files.move(tempFile.toPath(), targetFilePath, StandardCopyOption.REPLACE_EXISTING);
// 调用编译类进行编译
PublishResult result = publishProject(Paths.get(UPLOAD_DIR));
PublishResult result = publishProject(Paths.get(UPLOAD_DIR).getParent());
if (result.isSuccess()) {
// 找到发布后的 CxMesUploader.exe 文件
// 找到发布后的 radarcal 文件
Path publishedFilePath = findPublishedFile(result.getPublishDirectory());
if (publishedFilePath != null) {
// 返回发布后的文件
Resource resource = new FileSystemResource(publishedFilePath.toFile());
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=CxMesUploader.exe");
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=radarcal");
return ResponseEntity.ok()
.headers(headers)
.contentLength(resource.contentLength())
......@@ -78,6 +79,7 @@ public class FileUploadController {
private PublishResult publishProject(Path projectDir) throws IOException, InterruptedException {
// 定义发布目录
Path publishDir = projectDir.resolve("build");
System.out.println("publishDir: " + publishDir);
// 执行 rm -rf build && mkdir build
ProcessBuilder rmMkdirBuilder = new ProcessBuilder("bash", "-c", "rm -rf build && mkdir build");
......@@ -85,11 +87,14 @@ public class FileUploadController {
Process rmMkdirProcess = rmMkdirBuilder.start();
int rmMkdirExitCode = rmMkdirProcess.waitFor();
// 捕获并打印 rm -rf build && mkdir build 的输出
logProcessOutput(rmMkdirProcess);
if (rmMkdirExitCode != 0) {
// 获取错误流并读取错误信息
try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(rmMkdirProcess.getErrorStream()))) {
String errorLog = errorReader.lines().collect(Collectors.joining("\n"));
return new PublishResult(false, errorLog, publishDir);
String errorLog = errorReader.lines().collect(Collectors.joining(" "));
return new PublishResult(false, "Failed to clean and create build directory: " + errorLog, publishDir);
}
}
......@@ -99,22 +104,56 @@ public class FileUploadController {
Process cmakeMakeProcess = cmakeMakeBuilder.start();
int cmakeMakeExitCode = cmakeMakeProcess.waitFor();
String errorLog = "";
// 捕获并打印 cmake 和 make 的输出
String outputLog = logProcessOutput(cmakeMakeProcess);
if (cmakeMakeExitCode != 0) {
// 获取错误流并读取错误信息
try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(cmakeMakeProcess.getErrorStream()))) {
errorLog = errorReader.lines().collect(Collectors.joining("\n"));
String errorLog = errorReader.lines().collect(Collectors.joining(" "));
return new PublishResult(false, "CMake or Make failed: " + outputLog + " Error: " + errorLog, publishDir);
}
}
return new PublishResult(cmakeMakeExitCode == 0, errorLog, publishDir);
return new PublishResult(true, "Build succeeded", publishDir);
}
/**
* 捕获并打印进程的标准输出和错误输出。
*
* @param process 要捕获输出的进程
* @return 标准输出的日志内容
*/
private String logProcessOutput(Process process) throws IOException {
StringBuilder outputLog = new StringBuilder();
// 捕获标准输出
try (BufferedReader outputReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
System.out.println("Standard Output:");
while ((line = outputReader.readLine()) != null) {
System.out.println(line); // 打印到控制台
}
}
// 捕获错误输出
try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
String line;
System.out.println("Error Output:");
while ((line = errorReader.readLine()) != null) {
System.err.println(line); // 打印到控制台(红色字体)
}
}
return outputLog.toString();
}
private Path findPublishedFile(Path publishDir) {
File[] files = publishDir.toFile().listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile() && file.getName().equals("CxMesUploader.exe")) {
if (file.isFile() && file.getName().equals("radarcal")) {
return file.toPath();
}
}
......
.upload-container[data-v-f9a9c404]{display:flex;flex-direction:column;align-items:center}h3[data-v-ebbc34ee]{margin:40px 0 0}ul[data-v-ebbc34ee]{list-style-type:none;padding:0}li[data-v-ebbc34ee]{display:inline-block;margin:0 10px}a[data-v-ebbc34ee]{color:#42b983}
\ No newline at end of file
.upload-container[data-v-3d5ae34e]{display:flex;flex-direction:column;align-items:center}h3[data-v-ebbc34ee]{margin:40px 0 0}ul[data-v-ebbc34ee]{list-style-type:none;padding:0}li[data-v-ebbc34ee]{display:inline-block;margin:0 10px}a[data-v-ebbc34ee]{color:#42b983}
\ No newline at end of file
<!doctype html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/favicon.ico"><title>frontend</title><script defer="defer" src="/js/chunk-vendors.620ce23d.js"></script><script defer="defer" src="/js/app.d72c73f1.js"></script><link href="/css/chunk-vendors.10dd4e95.css" rel="stylesheet"><link href="/css/app.8803a0e3.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but frontend doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div></body></html>
\ No newline at end of file
<!doctype html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/favicon.ico"><title>frontend</title><script defer="defer" src="/js/chunk-vendors.620ce23d.js"></script><script defer="defer" src="/js/app.4cf479e6.js"></script><link href="/css/chunk-vendors.10dd4e95.css" rel="stylesheet"><link href="/css/app.9c706792.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but frontend doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div></body></html>
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment