1

我想从我的 Angular-Frontend 向 SpringBoot-Server 发送一个文件(如 4/5 MB)。我通过 StompJS 使用 STOMP,每当我尝试发送文件的内容时,我的客户端都会失去连接......(我认为是因为消息大小 => 小字符串有效)。

有没有办法将一个非常大的字符串或整个文件发送到我的 java 后端?我也尝试发送字节,但服务器端的结果是空文件。谢谢!

PS:我已经更改了服务器的缓冲区大小,所以问题一定出在 stompjs-client...

连接服务(前端):

  public setupConnection() {

        console.log("Initialize WebSocket Connection");
        let ws = new SockJS(this.webSocketEndPoint);
        this.stompClient = Stomp.over(ws);
        this.stompClient.maxWebSocketChunkSize = 10000000;
        this.stompClient.splitLargeFrames = true;
        const _this = this;
        _this.stompClient.connect({}, function (frame) {
            _this.stompClient.subscribe(_this.subscribeDestination, _this.stompClient.send("/app/userdata", {}, "data"), function (sdkEvent) {
                _this.onMessageReceived(sdkEvent);
            });
            //_this.stompClient.reconnect_delay = 2000;
        }, this.errorCallBack);
        
  }
  onMessageReceived(message) {
    console.log("Message Recieved from Server :: " + message);
    var result = JSON.stringify(message.body);
    console.log(result);
  }

  errorCallBack(error) {
    console.log("errorCallBack -> " + error )
    setTimeout(() => {
        this.setupConnection();
    }, 5000);
    
}

send(message) {
  console.log("Sending message...");
  this.stompClient.send("/app/config", {}, message);
  
}

public selfRegistration(){
  this.stompClient.send("/app/userdata",{}, "userdata");
}

文件输入处理(前端):

// as string
  onFileChangeString(event){
    this.file= event.target.files[0];
    const reader = new FileReader();
    reader.onload = (e) => {
 
        this.fileLoaded = true;
        this.filecontent = reader.result as String;
        this.connectionService.send(this.filecontent);
    }   
    reader.readAsText(this.file);
  }
  //as arraybuffer
  onFileChangeBuffer(event){
    this.file= event.target.files[0];
    const reader = new FileReader();
    reader.onload = (e) => {
 
        this.fileLoaded = true;
        this.filecontent = reader.result as ArrayBuffer;
        var array = new Uint8Array(this.filecontent);
        this.connectionService.send(array);
    }   
    reader.readAsArrayBuffer(this.file);
  }

消息处理(后端):

    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
        registry.setMessageSizeLimit(100000000);
        registry.setSendBufferSizeLimit(100000000);
    }

    //try string
    @MessageMapping("/config")
    @SendToUser("/ba/configuration")
    public String recieveConfigurationFileString(String message, @Header("simpSessionId") String sessionId, Principal principal) throws Exception {
        System.out.println("Received config message from" + message +" " + principal.getName() +" " + sessionId);
        
        return "testresult";
    }
    //try byte array
    @MessageMapping("/config")
    @SendToUser("/ba/configuration")
    public String recieveConfigurationFile(byte[] message, @Header("simpSessionId") String sessionId, Principal principal) throws Exception {
        System.out.println("Received config message from" + message +" " + principal.getName() +" " + sessionId);
      // how to handle byte input?         

        return "testresult";
    }

控制台输出是:

Sending message...
compat-client.js:32 >>> SEND
destination:/app/config
content-length:1970879


compat-client.js:32 chunk sent = 1969497, remaining = 0
compat-client.js:32 Connection closed to undefined
4

0 回答 0