Return CSV file from servlet using ServletOutputStream via HTTPS in Internet Explorer

I have a Servlet that returns a csv file that "works" through HTTP in Internet Explorer and Firefox. When I execute the same Servlet via HTTPS, only firefox continues to download the csv file via HTTPS. I do not think this is necessarily an Internet 6 or 7 issue described on MSDN :

Message:

Internet Explorer could not download data.csv from mydomain.com Internet Explorer could not open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.

Please note that after this message the site continues to "up", and you can continue browsing the site, namely downloading the CSV that requests this message. I managed to access similar files via https in IE from other j2ee applications, so I believe this is our code. Should I not close bufferedOutputStream?

UPDATE

to close or not to close the output stream: I asked this question on the java posse forums and discussion , there is also insight. In the end, it seems that no container should rely on the "client" (your servlet code in this case) to close this output stream. Therefore, if your refusal to close a thread in a servlet causes a problem, it is more a reflection of the poor implementation of your servlet container than your code. I posted the IDE and tutortials behavior from Sun, Oracle and BEA and how they are also incompatible in whether they close the stream or not.

About the specific behavior of IE . In our case, a separate product "Oracle Web Cache" introduced additional header values ​​that affect Internet Explorer only because IE implements the "No Cache" ("No Cache" requirement; see MSDN article ). The code:

public class DownloadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletOutputStream out = null; ByteArrayInputStream byteArrayInputStream = null; BufferedOutputStream bufferedOutputStream = null; try { response.setContentType("text/csv"); String disposition = "attachment; fileName=data.csv"; response.setHeader("Content-Disposition", disposition); out = response.getOutputStream(); byte[] blobData = dao.getCSV(); //setup the input as the blob to write out to the client byteArrayInputStream = new ByteArrayInputStream(blobData); bufferedOutputStream = new BufferedOutputStream(out); int length = blobData.length; response.setContentLength(length); //byte[] buff = new byte[length]; byte[] buff = new byte[(1024 * 1024) * 2]; //now lets shove the data down int bytesRead; // Simple read/write loop. while (-1 != (bytesRead = byteArrayInputStream.read(buff, 0, buff.length))) { bufferedOutputStream.write(buff, 0, bytesRead); } out.flush(); out.close(); } catch (Exception e) { System.err.println(e); throw e; } finally { if (out != null) out.close(); if (byteArrayInputStream != null) { byteArrayInputStream.close(); } if (bufferedOutputStream != null) { bufferedOutputStream.close(); } } } 
+6
java java-ee servlets
source share
1 answer

I’m really confused about your chest-to-head recording mechanism. Why not just (the servlet output stream will be bufferend, thats container stuff):

 byte[] csv = dao.getCSV(); response.setContentType("text/csv"); response.setHeader("Content-Disposition", "attachment; filename=data.csv")); reponse.setContentLength(csv.length); ServletOutputStream out = response.getOutputStream(); out.write(csv); 

There should also be no need to clean the output stream and close.

The contents of the header should not be parsed based on IE, but who knows: do not keep fileName . The next question is coding. CSV is text, so instead use getWriter( ) or g etOutputStream() and, for example, specify the content type "text / csv; charset = UTF-8". But dao should provide CSV as String instead of byte [].

The servlet code has nothing to do with HTTPS, so the protocol does not matter on the server side. You can check the servlet from localhost using HTTP, which I hope for.

What about filters in your application? A filter can also configure an HTTP header (or as a footer) using cache management, for example.

+4
source

All Articles