// The request's input stream.
InputStream is = null;
// The output stream to save uploaded data.
OutputStream os = null;
try {
// Get the upload as a binary file.
is = request.getBodyAsStream();
if(null == is) {
throw new BadRequestException("Request data is invalid");
}
// Save to output stream.
os = new BufferedOutputStream(new FileOutputStream(file));
byte[] buffer = new byte[8192];
int readBytes;
while(-1 != (readBytes = is.read(buffer))) {
if(0 < readBytes) {
os.write(buffer, 0, readBytes);
}
}
} finally {
if(null != is) {
try { is.close(); } catch(Exception ignored) { }
is = null;
}
if(null != os) {
try { os.close(); } catch(Exception ignored) { }
os = null;
}
}