简体   繁体   中英

How to improve file IO performance in a rest service

The scenario I have at hand is from my spring boot rest service, read a word doc from resources folder and pass the byte array to the client

I read the word doc in memory using FileInputStream, convert input stream to a byte array using Apache Common IO IOUtils and place it in the response body of the rest service.

The problem here is that I always read the file in memeirh oer service request which is detrimental for there local memory of the process where service is running on.

I can't read the file line by line and return it to the service caller in that fashion as I need to return the byte array back to the caller all together

Another problem I foresee is with how the file is read. I want to be a non blocking IO instead of a blocking IO.

Wondering what would be an efficient way to solve this

Do you actually need to read the file every time a request comes in.

Otherwise you could just read the file on server startup and then keep the file in memory stored in a Spring Bean. Then fetch it from there on every call?

If you don't want to upload file every time, it's better to create the @Bean , doing that in init/postconstruct phase. You also can add some functionality to your retrieve() method, which checks and stores file modification time with invokation of File.lastModified() to decide whether you have to reload the content or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM