Commit 89b2b532 authored by liuyan's avatar liuyan

fix:优化代码

parent 795ebdd6
......@@ -2,6 +2,7 @@ package com.ruoyi.client.service;
import com.google.protobuf.ByteString;
import com.ruoyi.grpc.file.*;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import org.springframework.stereotype.Service;
......@@ -21,27 +22,27 @@ public class FileServiceImpl extends FileServiceGrpc.FileServiceImplBase {
@Override
public void onNext(FileUploadRequest request) {
try {
if (request.hasFilename()) {
filename = request.getFilename();
try {
fos = new FileOutputStream("server_files/" + filename);
} catch (IOException e) {
responseObserver.onError(e);
}
fos = new FileOutputStream("E:\\server_file\\" + filename);
} else {
try {
byte[] chunk = request.getChunk().toByteArray();
totalSize += chunk.length;
fos.write(chunk);
} catch (IOException e) {
responseObserver.onError(e);
}
}catch (Exception e){
// 返回 INVALID_ARGUMENT 错误
responseObserver.onError(Status.INVALID_ARGUMENT
.withDescription("Upload failed:" + e.getMessage())
.withCause(e)
.asRuntimeException());
}
}
@Override
public void onError(Throwable t) {
System.err.println("Upload failed: " + t.getMessage());
System.err.println("客户端中断上传: " + t.getMessage());
}
@Override
......@@ -54,7 +55,10 @@ public class FileServiceImpl extends FileServiceGrpc.FileServiceImplBase {
.build());
responseObserver.onCompleted();
} catch (IOException e) {
responseObserver.onError(e);
// 最终错误处理
responseObserver.onError(Status.INTERNAL
.withDescription("Finalization failed: " + e.getMessage())
.asRuntimeException());
}
}
};
......
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