When retrieving large files from a database in Java, loading the entire content as a byte array causes OutOfMemoryError. The solution is to implement a custom DataSource that wraps an InputStream, then pass it to a DataHandler constructor. This avoids loading all data into memory at once. The tutorial walks through creating an InputStreamDataSource class implementing the DataSource interface, using it to build a DataHandler, and optionally supporting dynamic MIME type specification. Note that since Java 11, the Java Activation Framework (javax.activation) must be added as an explicit dependency.

6m read timeFrom feeds.feedblitz.com
Post cover image
Table of contents
1. Introduction2. Understanding the InputStream Problem3. Using getBinaryStream4. Working of a DataHandler5. Conclusion

Sort: