Commit 5b1541a6 authored by liuyan's avatar liuyan

fix:增加配置文件

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