JavaFX – Upload File

Bookmark and Share

One of the previous post had a sample to download large files using JavaFX. It relies on HttpRequest attribute sink to specify the output file location. Similarly we can use source attribute for uploading a file.

I have written a simple UploadServlet to receive the file content and save at <user.home>/JavaFXDownloads/ location.

Relevant part of JavaFX client code is given below..

def uploadServletURL = 

def urlConverter = URLConverter{ };
def encodedServletURL = urlConverter.encodeURL(

def httpRequest: HttpRequest = HttpRequest {

    location: encodedServletURL    
    source: new
    method: HttpRequest.POST

    headers: [
        HttpHeader {
            name: HttpHeader.CONTENT_TYPE
            value: "multipart/form-data"

Name of file is passed as argument to upload servlet. This URL is encoded using URLConverter. The file to be uploaded is assigned to source attribute. Http content-type is set to “multipart/form-data”. You can host the UploadServlet – server – code in any webserver such as Tomcat, Glassfish etc. Value of uploadServletURL in JavaFX client must be updated to point to this servlet URL.

I found another sample code Multipart HTTP file upload with JavaFX which demonstrates alternate approach.

Try it out and let me know feedback..

About Rakesh Menon

13 Responses to JavaFX – Upload File

  1. Pingback: JavaFX links of the week, February 8 // JavaFX News, Demos and Insight // FX Experience

  2. Pingback: Java desktop links of the week, February 8 | Jonathan Giles

  3. Pingback: JavaFX Links of the Week 2, February 2010 | Herbert Mühlburger's Weblog

    • Frank Gao says:

      I tried to use your sample code to upload a file to my http://localhost/. It did not show any error. However, I could not find the uploaded file in my localhost directory.
      I’m using XAMPP 1.7.2 in Windows. The access.log says “ – – [15/Feb/2010:23:22:36 -0600] “POST / HTTP/1.1” 200 254 “-” “Mozilla/4.0 (Windows XP 5.1) Java/1.6.0_17”
      It seems to be fine. I do not what happened here. Please give me your advice.
      Thanks lot in advance.

      • Rakesh Menon says:

        @Frank Gao Can you provide some more information on what happened on server side…
        Are you using UploadServlet from this blog? Did the servlet receive the request?
        Is it able to get the file name, but fails to retrieve the data?

  4. Frank Gao says:

    Thank you very much for your response.

    I did not use your servlet yet. I just established the client side. On the server side, the only information I got was from Apache log file. The message was “ – – [16/Feb/2010:21:36:12 -0600] “POST / HTTP/1.1” 200 254 “-” “Mozilla/4.0 (Windows XP 5.1) Java/1.6.0_17”. I did not get the file name and contents.

    Do I have to use servlet to establish HttpRequest? Does host company allow this kind servlet works on their web server? Please give me a confirmation.


  5. Frank Gao says:

    Thanks again for your help.

    I have built the Servlet and deployed to my Tomcat. Basically I copied the “UploadServlet.war” file to C:/xampp/tomcat/lib”. I set the URL= “http://localhost:8080/UploadServlet” in client side. However, when I ran the client side app, I got the following error
    “onException – http://localhost:8080/UploadServlet“.
    Could you point out the problem?

  6. Frank Gao says:

    Thanks a lot for your help.
    Following your instruction, I copied UploadServlet.war to “tomcat/webapps”. If I put URL as “http://localhost:8080/UploadServlet”, I got the “index.jsp” page with message “Upload Servlet”. It seems to be okay to access the servlet. However, when I tried to upload a “jpg” file, I got nothing at server directory. I checked the log file in “tomcat”. There was no error. I did not see error message in java console window either.
    1. Do you know what happened?
    2. where is the directory of “user.home”?

  7. Frank Gao says:

    Finally it works now. Thank you very much.
    You are right. The URL should be “http://localhost:8080/UploadServlet/UploadServlet”
    In addition, I had to fix one issue in the source “” as the following
    printWriter.println(” SUCCESS”);
    The correct code should be
    printWriter.println(” SUCCESS”);

    Thanks again for your help.

  8. Rakesh Menon says:

    @Frank Gao Glad.. Thanks for trying it out..

Leave a Reply

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

You are commenting using your 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: