JavaFX – Upload File
February 3, 2010 13 Comments
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..
|
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..




Pingback: JavaFX links of the week, February 8 // JavaFX News, Demos and Insight // FX Experience
Pingback: Java desktop links of the week, February 8 | Jonathan Giles
Pingback: JavaFX Links of the Week 2, February 2010 | Herbert Mühlburger's Weblog
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.
@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?
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,
Can you try running the servlet along with the JavaFX app. You can deploy it on Tomcat/Glassfish.
This particular servlet needs webserver like Tomcat, Glassfish etc The upload logic depends a lot on the server. Need to find details of how the server handles the upload request.
Example: Using flickr photo upload API…
http://www.flickr.com/services/api/upload.api.html
http://javafx.com/samples/ScreenshotMaker/index.html
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 – java.io.FileNotFoundException: http://localhost:8080/UploadServlet“.
Could you point out the problem?
Regards,
@Frank Gao I think the servlet is not deployed properly. Once you deploy can you try accessing the URL from browser? I think you must be able to copy the contents of the war under “tomcat/webapps/server” and access the URL as http://localhost:8080/server/UploadServlet You can refer to Tomcat deployment docs for more information.
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”?
Thanks.
@Frank Gao Most likely the URL of the Servlet is different, may be its http://localhost:8080/UploadServlet/UploadServlet
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 “UploadServlet.java” as the following
printWriter.println(” SUCCESS”);
The correct code should be
printWriter.println(” SUCCESS”);
Thanks again for your help.
@Frank Gao Glad.. Thanks for trying it out..