The problem is at below line where you are writing no of bytes not actual bytes. Here readBytes
represents no of bytes read at a time where as buffer
contains actual bytes that is read.
response.getOutputStream().write(readBytes);
Try
OutputStream outputStream = response.getOutputStream();
while ((readBytes = fileToDownload.read(buffer)) != -1) {
outputStream.write(buffer,0,readBytes);
}
outputStream.close();
I suggest you to call response.getOutputStream()
single time.
Your code will give you IndexOutOfBoundsException
if the size of the file is less than 10000 bytes because of below line
fileToDownload.read(buffer, 0, 10000)
Change it to
fileToDownload.read(buffer)
Use ServletContext
to get file path.
ServletContext context = getServletContext();
For more info have a look at below posts: