We can upload or download large files using new HttpRequest attributes available in JavaFX 1.2.

HttpRequest has input and onInput attributes that provides InputStream which can be used to download data. This stream supports mark, reset, available, skip etc. But to support this HttpRequest buffers the data. Due to this buffering it was not possible to use this approach to download large files.

JavaFX 1.2 introduce two new attributes - source (InputStream) and sink (OutputStream). If we set sink attribute, we can directly download the content of stream without buffering. Also with source we can upload the content. Eg: By setting it to FileInputStream. When source or sink attribute is used, corresponding input, onInput, output, onOutput will not be functional.

For Applet mode, click on above image

For standalone mode

The application downloads a larger version of the photo. I took it from Somnathpur using Nikon Coolpix! :)

function downloadFile(url , outputFile) {

    def getRequest: HttpRequest = HttpRequest {

        location: url
        sink: new java.io.FileOutputStream(outputFile)

        onToRead: function(bytes: Long) {
            toRead = bytes;
            println("onToRead({bytes})");
        }

        onRead: function(bytes: Long) {
            read = bytes;
            println("onRead - {read * 100/toRead}%");
        }

        onDone: function() { println("onDone") }
    }

    getRequest.start();
}

In above sample, sink is assigned to a FileOutputStream. So all the content will be directly written to file without buffering. Note: For making the application compatible with mobile or other platforms we will have to use only subset of java.io package. Example: MID Profile Core API.

Source:



Comments:

For some reason, I can't get the upload progress. Download progress values (like read etc.) are ok, but not when uploading.

I can see the uploading is working (and the onDone method is called) is this a bug?

Posted by Arno Raps on June 24, 2009 at 05:24 PM IST #

@Arno Raps For POST request, you can listen to onWritten which provides the total bytes written to stream. onToWrite will return -1, the application needs to compute the total bytes that will be written.

Posted by Rakesh Menon on June 25, 2009 at 11:45 AM IST #

Thanks for answering, but the problem is when I use the source method:
source: new java.io.FileInputStream("d://somebigfile");

I get a:
onConnecting
onDoneConnect
onWritten: 65536
onWritten: 131072
onWritten: 196608
...
onWritten: 2236696
onReadingHeaders
****************************************
Progress: 2236696
written: 2236696

So it looks like the source stream is buffered anyway (I can see in my netwerk that the upload is busy for a while after this)

Posted by Arno Raps on June 25, 2009 at 02:51 PM IST #

@Arno Raps It does create a byte array of 64kb, but will not buffer the entire content before upload or download. For desktop implementation in this case it creates a URLConnection and writes directly to its OutputStream

Posted by Rakesh Menon on June 25, 2009 at 03:25 PM IST #

But how can I monitor its progress than? the onWritten isn't the correct value of the real bytes send thru the network. I would like to have such a progress to report to the client, just like the progress bar in your download example.

Posted by Arno Raps on June 25, 2009 at 03:39 PM IST #

@Arno Raps onWritten does provide the correct value every 64kb, which is fine for large files. For small files instead of specifying "source", obtain the OutputStream from onOutput. Also if you use "source" then there is no way to find the size of total bytes of data that will be written (as there is no buffering). In case of read, the total bytes to be read is obtained from Content-Length HTTP header.

Posted by Rakesh Menon on June 25, 2009 at 05:15 PM IST #

In my code, the onWritten events are long done when the upload is working:
onConnecting
onDoneConnect
onWritten: 65536
onWritten: 131072
onWritten: 196608
...
onWritten: 2236696
onReadingHeaders

(this was a 2.2 MB file upload. These lines (in 64K increments al happend in 1 second, while de file upload takes several minutes ( a 448 kbps upstream here )

Posted by Arno Raps on June 25, 2009 at 06:57 PM IST #

@Arno Raps I discussed issue with developer. Please file a bug with this testcase, so that he can investigate further. It may be due to delayed response from server. But need to do some further investigation.
http://javafx-jira.kenai.com/secure/Dashboard.jspa

Posted by Rakesh Menon on June 26, 2009 at 10:44 AM IST #

I've made a bug issue:
You have successfully created the issue (RT-5027), however you do not have the permission to view the created issue.

Posted by Arno Raps on June 26, 2009 at 01:45 PM IST #

@Arno Raps Thanks a lot.. I have updated bug to make it public. Hope now you can view the issue. It will be reviewed by the developer.

Posted by Rakesh Menon on June 26, 2009 at 02:37 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed

This blog copyright 2009 by Rakesh Menon