bhard
bhard

Reputation: 21

read file using nio and write to servlet

I wan to read a file and then write using java.nio and return it to servlet.i also want to check whether file has been downloaded or not.plz suggest.

Upvotes: 0

Views: 415

Answers (1)

Geoff
Geoff

Reputation: 3129

java.nio is notoriously overcomplicated if you're going to block the thread anyway.

You'd be far better just using the stream based io services and creating a copyStream method which takes an input stream and an output stream.

If you're talking about a client application, and you'd like to send data to a servlet, then just create a new thread to do the stream copying, if you're talking about a Servlet application, and you want to return this file to the client, then you're already running a separate thread, and you can just start the copyStream method copying the file to the ServletOutputStream directly.

As for determining if the file was downloaded or not, you can only make a best guess. For example, if you encounter any errors while copying the stream, then you can safely assume that the file failed, but if you don't encounter any errors then you don't really know if it completed successfully or not. In all likely hood it did, but you can't be sure.

Upvotes: 1

Related Questions