Tuesday, 8 August 2017

Handling Internet Explorer error "File couldn’t be downloaded" in J2EE application

This error was one of a stubborn errors due to insufficient and misleading information on the internet. On the face of it, the issue resembles with the IE settings. Furthermore, the file downloads when the application is access directly on server browser or other client browsers through FireFox, this issue appears on Chrome and IE 11.




 Initially, I found following as the solutions:
  • using a login with Admin rights
  • disabling download manager
  • disabling internet security or you antivirus
  • Windows Firewall On/Off
  • In-Activating the option "DO NOT SAVE ENCRYPTED FILES TO DISK " in internet explorer settings.
  • Messing with Windows Registry. For e.g. setting the value to 1 for ScanWithAntivirus at location HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Attachments
After trying all the option provided above and have no luck what it appeared to me that it is something with the response of the server that needs to be pushed. Once again I dived into the code which apparently looks OK to me initially. 


What flush() method do according to the Java documentation is : 

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination. 

And the second paragraph that leads me to the conclusion of the solution:

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive. 

What here is missing is close() method which ensures that the output stream is close and release any system resources with the stream. In documentation, it states,

Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

Therefore, following code fixes the problem:



P.S. For .net users the Response.End() should work. Any comments, related to this topic is highly appreciated which increases and helps the information of readers. Cheers.. 😃