tags:

views:

64

answers:

1

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" />
A: 

And why are you taking the intValue() of it? You're already well into 8 hex digits with 288 MB, i.e. when the file size gets large enough to need the next digit, it will overflow the int. Use the entire 'long'. Better still, don't specify it at all, let Tomcat take care of it.

Also make sure you are using chunked transfer mode.

EJP