Quiero que los usuarios de mi aplicación web descarguen algunos datos como un archivo de Excel.Cómo puedo obtener un flujo de entrada del objeto HSSFWorkbook
Tengo la siguiente función para enviar un flujo de entrada en el objeto de respuesta.
public static void sendFile(InputStream is, HttpServletResponse response) throws IOException {
BufferedInputStream in = null;
try {
int count;
byte[] buffer = new byte[BUFFER_SIZE];
in = new BufferedInputStream(is);
ServletOutputStream out = response.getOutputStream();
while(-1 != (count = in.read(buffer)))
out.write(buffer, 0, count);
out.flush();
} catch (IOException ioe) {
System.err.println("IOException in Download::sendFile");
ioe.printStackTrace();
} finally {
if (in != null) {
try { in.close();
} catch (IOException ioe) { ioe.printStackTrace(); }
}
}
}
me gustaría transformar mi objeto HSSFWorkbook a un flujo de entrada y pasarlo al método anterior.
public InputStream generateApplicationsExcel() {
HSSFWorkbook wb = new HSSFWorkbook();
// Populate the excel object
return null; // TODO. return the wb as InputStream
}
http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html
+1 por ejemplo. ¿Pero cómo puedo obtener el tamaño de archivo de HSSFWorkbook? Quiero agregar un encabezado 'Content-Length' – MyTitle
@MyTitle. Creo que si quisieras configurar un encabezado de longitud de contenido necesitarías primero escribir la secuencia en un ByteArrayOutputStream, luego obtener su longitud y luego escribirla en el HTTP respuesta. –