Commit 5b1541a6 authored by liuyan's avatar liuyan

fix:增加配置文件

parent 89b2b532
......@@ -4,6 +4,7 @@ import com.google.protobuf.ByteString;
import com.ruoyi.grpc.file.*;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.FileInputStream;
......@@ -13,6 +14,9 @@ import java.io.IOException;
@Service
public class FileServiceImpl extends FileServiceGrpc.FileServiceImplBase {
@Value("${server.upload.base_path}")
String uploadBasePath;
@Override
public StreamObserver<FileUploadRequest> uploadFile(StreamObserver<FileUploadResponse> responseObserver) {
return new StreamObserver<FileUploadRequest>() {
......@@ -25,13 +29,21 @@ public class FileServiceImpl extends FileServiceGrpc.FileServiceImplBase {
try {
if (request.hasFilename()) {
filename = request.getFilename();
fos = new FileOutputStream("E:\\server_file\\" + filename);
// 创建文件对象
java.io.File ioFile = new java.io.File(uploadBasePath + filename);
// 创建文件所在的目录
ioFile.getParentFile().mkdirs();
// 创建文件,如果文件存在则覆盖
ioFile.createNewFile();
fos = new FileOutputStream(ioFile);
} else {
byte[] chunk = request.getChunk().toByteArray();
totalSize += chunk.length;
fos.write(chunk);
}
}catch (Exception e){
e.printStackTrace();
// 返回 INVALID_ARGUMENT 错误
responseObserver.onError(Status.INVALID_ARGUMENT
.withDescription("Upload failed:" + e.getMessage())
......@@ -50,11 +62,12 @@ public class FileServiceImpl extends FileServiceGrpc.FileServiceImplBase {
try {
fos.close();
responseObserver.onNext(FileUploadResponse.newBuilder()
.setStatus("Success")
.setStatus(Status.OK.toString())
.setSize(totalSize)
.build());
responseObserver.onCompleted();
} catch (IOException e) {
e.printStackTrace();
// 最终错误处理
responseObserver.onError(Status.INTERNAL
.withDescription("Finalization failed: " + e.getMessage())
......@@ -71,11 +84,12 @@ public class FileServiceImpl extends FileServiceGrpc.FileServiceImplBase {
StreamObserver<FileDownloadResponse> responseObserver) {
try {
java.io.File file = new java.io.File("server_files/" + request.getFilename());
java.io.File file = new java.io.File(uploadBasePath + request.getFilename());
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[64 * 1024]; // 64KB chunks
int bytesRead;
//循环输入流,读取fis赋值给buffer,传输给客户端
while ((bytesRead = fis.read(buffer)) != -1) {
responseObserver.onNext(FileDownloadResponse.newBuilder()
.setChunk(ByteString.copyFrom(buffer, 0, bytesRead))
......
......@@ -47,6 +47,9 @@ user:
# Spring配置
spring:
# 引入项目自定义文件
config:
import: "optional:classpath:server-config.yml"
# 资源信息
messages:
# 国际化资源文件路径
......@@ -127,3 +130,5 @@ xss:
excludes: /system/notice
# 匹配链接
urlPatterns: /system/*,/monitor/*,/tool/*
server:
upload:
base_path : E:\server_file\ #文件上传至服务器的路径
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