I have a web interface that is used to upload a file. When the request arrives, my Glassfish server transfers the file from the web service and then writes the contents to the output stream. My code works fine, except when the file size becomes very large (for example, more than 200 MB), it hangs, showing 0% downloaded in the browser, and the file never loads.
When I move the flush () method inside a while loop, it works fine for large files as well. I am not sure if the flush () problem in the loop is the problem. Not sure how this thing really works. My code is as follows:
HttpURLConnection conn = (HttpURLConnection) downloadUri.toURL().openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/pdf");
if (conn.getResponseCode() == 200) {
ServletOutputStream output;
try (InputStream inputStream = conn.getInputStream()) {
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.setContentType("application/octet-stream");
response.setHeader("Content-Length", conn.getHeaderField("Content-Length"));
response.setHeader("Content-Disposition", "attachment; filename=\"" + abbr + ".pdf\"");
output = response.getOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
}
output.flush();
output.close();
Any thoughts ?. Thanks for looking at this.
vinay