In my Servlet I would need to propose a file download to the end user and also display an HTML page indicating the status of the download.
I read that it was not possible to use both OutputStream and PrintWriter on the same response, but by reading this post, I tried to make it working...but no success...
I tried the following:
try(InputStream in = new FileInputStream(new File(bf.path));
ServletOutputStream out = response.getOutputStream()) {
byte[] buffer = new byte[1048];
int numBytesRead;
while ((numBytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, numBytesRead);
}
out.flush();
PrintWriter print = new PrintWriter(new OutputStreamWriter(out, "utf-8"));
print.println("<html>");
print.println("</html>");
print.flush();
print.close();
}
The download is proceeded successfully but my HTML content is never displayed...I also tried to perform the PrintWriter before the file download but same result...no HTML content displayed.
I also tried with
PrintStream print = new PrintStream(out, true, "utf-8");
but same thing... any idea on how I can propose a file download and display HTML content on the same Servlet ?