I have some problem with downloading using this servlet code which makes use of Tomcat NIO and Sendfile:
long fileSize = file.length();
long startAt = 0;
if (request.getHeader("Range") != null) {
response.setStatus(206);
startAt = Long.parseLong(request.getHeader("Range").replaceAll("bytes=", "").split("-")[0]);
}
long dataToWrite = fileSize;
if (startAt > 0) {
response.setHeader("Content-Range", String.format("bytes - %d-%d/%d", startAt, fileSize - 1, fileSize));
dataToWrite = fileSize - startAt;
}
request.setAttribute("org.apache.tomcat.sendfile.filename", file.getCanonicalPath());
request.setAttribute("org.apache.tomcat.sendfile.start", startAt);
request.setAttribute("org.apache.tomcat.sendfile.end", fileSize);
response.setContentLength(Long.valueOf(dataToWrite).intValue());
It's successfully work with file abou 20Mb. But when I'm trying to download file with size about 288Mb I see empty file. The size of downloaded file is 0 byte. I use jre6, tomcat 6.x with NioConnector:
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443" useSendfile="true" />