What type is the best to manage binary data in Java?

后端 未结 5 1469
时光说笑
时光说笑 2021-02-13 03:30

I\'m getting some documents from the web and many of them are binary files (executables, PDF, etc.). In Java, what is the correct type to hold the binary data until save it in d

5条回答
  •  我在风中等你
    2021-02-13 04:12

    For small amount of data use a byte[] but for binary files (to be stored in database BLOBs) you should use a temporary file as InputStream. JavaEE also does this for uploaded files. It is not good for the server performance to waste memory for byte[]. Imagine a webapp delivering ten PDF file each about 200MB. The server will need more than 2GB of RAM just for the webapp.

    Also using an InputStream allows JDBC to stream the data to the database (for most JDBC drivers, not for MySql, which will hold the data in memory two more times for client and server).

    You may have a look on Apache Commons FileUpload and setBlob() of PreparedStratement.

提交回复
热议问题