Commit 89b2b532 authored by liuyan's avatar liuyan

fix:优化代码

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