1

everyone. I am using apache poi to generate excel format file, I didn't store it on my disk, just byte array, then I use outputstream to write the byte array to client, so the client can download the excel file. Something like this:

OutputStream outputStream = response.getOutputStream();
byte[] fileData = generateDownloadFile(paramters);
outputStream.write(temp);
outputStream.flush();

However, I'm wondering if there exists a method that I can append the byte array to client instead of write it once, so I can generate some byta array then flush them, then generate some and flush them, and finally close connection. If using outputstream's write method doing this, the later overwrites the former.

Why I need this? I just want to send something back to client in a short duration, if I generate the whole excel file byte array, then flush it back, the connection may be already closed and cause a 504 error.

I haved tried:

  • append method in PrintWriter class. But it didn't work, since I need to send byte instead of character, I cannot open the file using MSExcel when through PrintWriter.
  • I also tried to use socket. But socket still need to use outputstream, like new Socket().getOutputStream();.

I'm an expert in Java, even not familiar. Can anyone help to figure out? Thanks.

uncle liu
  • 37
  • 7
  • "If using outputstream's write method doing this, the later overwrites the former." How do you know this? Because outputstream's write method just sends additional data, appending it implicitly to the already sent data. – Thomas Kläger Oct 24 '18 at 05:11
  • @ThomasKläger Well, first, I store my data in a List when I get them from db, then I loop through the list through index, using subList method to get a piece of data one time, then generate byte array of excel format, then using `outputstream.write` and `outputstream.flush`,to send them to browser. – uncle liu Oct 24 '18 at 05:34
  • @ThomasKläger You remind me that may be I slice the list in a wrong index, I will check it. – uncle liu Oct 24 '18 at 05:35
  • @ThomasKläger Well, that's really append method. The problem is, only the first chunk data that flushed to client is written in Excel format, others are not written in the file. When I first look the file in explorer, it shows 20kb, but when I open it then close, it shows 8kb. – uncle liu Oct 24 '18 at 09:26

1 Answers1

0

Output has a write method to add bytes with an offset:

   outputstream.write(bytes, alreadySentSizeInBytes, 
                                          bytes.length);

Please check if this works

Mohan
  • 334
  • 2
  • 12
  • Thanks for reply. I checked the document, it's only `write(byte[] bytes, int off, int len)`, the second parameter is not the already sent size in bytes, but is the start point of bytes. _element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation._ – uncle liu Oct 24 '18 at 06:13
  • 1
    @uncle Liu.... When you find the number of bytes previously added, you can use that as the offset. I have used this method for appending all through my life. Please try it..it would work – Mohan Oct 24 '18 at 06:37
  • Thanks. I didn't try it, but I found the write method would append the data to client automatically, so the problem is not what I thought it should be. The problem is, after the first chunk data sent back to client, other data did not organized in excel form, although they have been sent back. – uncle liu Oct 24 '18 at 09:46