Home > javafx > JavaFX – Upload and Download Large Files

JavaFX – Upload and Download Large Files

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;

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

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


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.


Categories: javafx Tags: , , ,
  1. June 24, 2009 at 4:54 AM

    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?

  2. June 24, 2009 at 11:15 PM

    @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.

  3. June 25, 2009 at 2:21 AM

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

    onWritten: 2236696
    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)

  4. June 25, 2009 at 2:55 AM

    @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

  5. June 25, 2009 at 3:09 AM

    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.

  6. June 25, 2009 at 4:45 AM

    @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.

  7. June 25, 2009 at 6:27 AM

    In my code, the onWritten events are long done when the upload is working:
    onWritten: 65536
    onWritten: 131072
    onWritten: 196608

    onWritten: 2236696
    (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 )

  8. June 25, 2009 at 10:14 PM

    @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.

  9. June 26, 2009 at 1:15 AM

    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.

  10. June 26, 2009 at 2:07 AM

    @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.

  11. Gabriel
    December 3, 2009 at 11:28 AM

    Hi Rakesh,
    Currently I have been working with the HttpRequest trying to upload large files but I have had some problems to upload the file because the file is not received by my servlet. I dont know if I am missing something in the HttRequest declaration, below my HttpRequest code:
    uploadRequest = HttpRequest{
    location: url
    headers : [HttpHeader {
    value: "*/*"},
    HttpHeader {
    value: "multipart/form-data;"},
    HttpHeader {
    value: "keep-alive"},
    HttpHeader {
    value: "{"no-cache"}"
    HttpHeader {
    value: "{"ISO-8859-1,utf-8;q=0.7,*;q=0.7"}"
    name: "Content-Disposition:"
    value: "form-data; name="upfile";},
    {name: "Content-Type:"
    value: "application/octet-stream"}]
    source: new java.io.FileInputStream(file)
    method: HttpRequest.POST
    onWritten:function(size: Long):Void {println("onWritten – {size} bytes has now been written");}
    onException: function (ex:java.lang.Exception):Void { println("onException – exception: {ex.getClass()} {ex.getMessage()}");}
    onDone: function():Void { println("onDone");stop();}
    onToWrite: function(bytes:Long){println("onToWrite");}
    onWriting: function(){println("onWriting");}
    onRead: function(bytes: Long):Void {
    println("onRead – bytes read: {bytes}");
    onInput: function(is: java.io.InputStream):Void {println("onInput");
    try { println("onInput – bytes of content available: {is.available()}"); } finally { is.close();}
    onOutput: function(os:OutputStream):Void{
    Thanks in advance.

  12. Rakesh Menon
    December 14, 2009 at 11:54 PM

    @Gabriel Can you provide a bit more info? When you say "file is not received by my servlet", is the request received by servlet? Does it work fine if you use onInput (for small files)?

  13. December 25, 2009 at 5:55 AM

    Hi Rakesh
    I am working on how to upload files with the HttpRequest in javafx.I just began to learn javafx a couple of weeks ago.I want to use it to upload files in my programe,but I have a big problem.Can you give me a example of uploading images from a client to a server?
    thanks in advance

  14. Ted
    December 26, 2009 at 6:56 PM

    Hi Rakesh
    I got an other question now.
    I want to use the javafx programe in the browser,but there are a few questions.I can’t open local resourses in this programe.I guess there is something related to permission,can you give me a way to solve it?

  15. December 29, 2009 at 7:43 PM

    @Mark This depends on how the server is implemented. I can provide another sample including a simple server side jsp/servlet.

  16. December 29, 2009 at 7:46 PM

    @Ted For accessing local resources the application needs to be signed.
    Assuming you are using NetBeans, you can goto Project-Properties -> Application -> Check "Self Signed Jar". Then rebuild the application.

  1. February 3, 2010 at 7:01 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: