2016-09-06 98 views
2

在Java Spring框架中,如何创建一个端点,在请求结束之前开始使用块请求HTTP请求正文?Java Spring框架,在请求结束之前开始使用HTTP请求主体

看起来像是默认的行为,即终止方法直到请求结束才执行。

以下Node.js服务器开始使用请求体,如何对Java Spring框架执行相同操作?

const http = require('http'); 

const server = http.createServer((request, response) => { 
    request.on('data', (chunk) => { 
    console.log('NEW CHUNK: ', chunk.toString().length); 
    }); 

    request.on('end',() => { 
    response.end('done'); 
    }); 
}); 

server.listen(3000); 

输出:

NEW CHUNK: 65189 
NEW CHUNK: 65536 
NEW CHUNK: 65536 
NEW CHUNK: 65536 
NEW CHUNK: 54212 

回答

1

我不知道有映射解决方案分块弹簧我要做的要求是这样的:

package fr.viveris.xchange.cloud.storage.controller.synchro.v1; 

import java.io.IOException; 
import java.io.InputStream; 
import java.util.Arrays; 

import javax.servlet.http.HttpServletRequest; 

import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 

@Controller 
public class ChunkController { 

private static final int EOS = -1; 

    @RequestMapping(method = RequestMethod.POST) 
    public ResponseEntity<Void> upload(final HttpServletRequest request, @RequestParam final int chunkSize) { 
     try (InputStream in = request.getInputStream()) { 
      byte[] readBuffer = new byte[chunkSize]; 
      int nbByteRead = 0; 
      int remainingByteToChunk = chunkSize; 
      while ((nbByteRead = in.read(readBuffer, chunkSize - remainingByteToChunk, remainingByteToChunk)) != EOS) { 
       remainingByteToChunk -= nbByteRead; 
       if (remainingByteToChunk == 0) { 
        byte[] chunk = Arrays.copyOf(readBuffer, readBuffer.length); 
        remainingByteToChunk = readBuffer.length; 
        // do something with the chunk. 
       } 
      } 
      if (remainingByteToChunk != chunkSize) { 
       byte[] lastChunk = Arrays.copyOf(readBuffer, readBuffer.length - remainingByteToChunk); 
       // do something with the last chunk 
      } 
      return new ResponseEntity<>(HttpStatus.OK); 
     } catch (IOException e) { 
      return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); 
     } 
    } 
} 

,或者你可以为块的大小定义一个常量。

您也可以忽略块的大小,只处理in.read的结果直到流结束。