REST: download large files

I am wondering what is the recommended way to upload a large file using REST.

If the time spent on the server to write the file to disk can be large, should I wait for the write operation to complete before sending a response to 201 to the client?

Perhaps it is better to answer the user "on the fly" and process the write operation later? In this second case, what http status should I return? In fact, although I return 201, no one can guarantee a successful write operation.

+4
source share
1 answer

It sounds like you are not starting to write to disk until you read the last byte from HTTP. This has two undesirable effects:

  • IO , , . -.

. , . , 201.

API HTTP , , API.

, Java Servlet API InputStream :

InputStream content = request.getInputStream();

// do something better than this
OutputStream out = new FileOutputStream("content.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
    // whatever processing you want here
    out.write(buffer, 0, len);
}

// now you can send the 201

, InputStream , TCP (*). . , , . , .


(*) . . ,

+4

All Articles