Reputation: 20591
How I can get file size of Apache's POI HSSFWorkbook
?
I have code like
// inside Servlet
OutputStream out = response.getOutputStream();
hSSFWorkbook.write(out);
And I want to add Content-Length
header to response.
Upvotes: 3
Views: 2857
Reputation: 48356
Best bet is to buffer it somewhere, eg
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
int length = baos.size();
// Send length...
// Send data
OutputStream out = response.getOutputStream();
out.write( baos.toByteArray() );
out.close();
You could use a temp file instead if you wanted to avoid using memory to buffer in
POI will only calculate the size at the time it needs to write everything out, as working out the sizing is almost half the work of writing out! So, you're best off getting the size and the serialisation in one go
Upvotes: 4