Home > javafx > JavaFX – Upload File

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 = 
    
"http://localhost:8080/server/UploadServlet&#34;;

def urlConverter = URLConverter{ };
def encodedServletURL = urlConverter.encodeURL(
    "{uploadServletURL}?file={inputFile.getName()}")

def httpRequest: HttpRequest = HttpRequest {

    location: encodedServletURL    
    source: new java.io.FileInputStream(inputFile)
    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..

Advertisements
Categories: javafx Tags: ,
  1. Frank Gao
    February 17, 2010 at 11:10 AM

    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 “127.0.0.1 – – [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.

    Regards,

  2. Frank Gao
    February 16, 2010 at 11:17 AM

    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 “127.0.0.1 – – [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.

  3. February 16, 2010 at 12:07 PM

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

  1. February 8, 2010 at 2:20 AM
  2. February 8, 2010 at 2:20 AM
  3. February 9, 2010 at 3:12 AM

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: