[英]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.