簡體   English   中英

如何找到頁面的內容長度<entity>作為 http header 傳遞?</entity>

[英]How to find the content-length of Page <Entity> to be passed as the http header?

我正在使用 spring 引導和 Netflix Eureka。 我需要找到 Page 的總長度來設置 HTTP 響應 header 中的內容長度。 我的controller是這樣的……

Page<exampleEntity> examplePage = null;
examplePage=exampleService.getFuntion(request, params);
HttpHeaders headers = new HttpHeaders();
headers.setContentLength(???);
return new ResponseEntity<>(Page, HttpHeaders ,HttpStatus.OK);

...

有人可以幫忙嗎?

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = null;
try {
  out = new ObjectOutputStream(bos);   
  out.writeObject(yourObject);
  out.flush();
  byte[] yourBytes = bos.toByteArray();
  
} finally {
  try {
    bos.close();
  } catch (IOException ex) {
    // ignore close exception
  }
}

那么 yourBytes.length 就是你需要的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM