Simple java ftp upload and resize pics droplet & applet
Update3 (05 February 2008): now the applet doesn't upload empty folders
Update2 (04 February 2008): fixed a bug that prevented files with uppercase extension to be loaded
Update: all bug fixed, internationalized, run as as application and as an applet too
I've done a "droplet" that upload and resize pictures and folders with FTP.
Why?
Since I was looking for a simple and opensource "droplet" application, and I haven't found one.
The requisites was quite simple:
- upload files of some Mbs
- resize large pictures to speed upload
- SIMPLE: use drag&drop
- upload folders too
This is the result:
Download applet/droplet + sources
To use it:
- Download the jar+html example
- open sample.html
- Drag image files and folders of pictures
- Upload
In net\madarco\lightview\UploadApplet\FtpParameters.properties (in the jar, use winrar to open it) you can configure the ftp parameters. Beware that the user&pass would be accessible in the .jar, so don't deploy this applet in a public area.
In the next releases (if there will be) I could implement an Http upload or prompt the user for the password.
This applet is released as Public Domain, use it as you will. In the jar you'll find the source. Hope you'll enjoy it.
Know bugs:
Can upload only once
Can't run as standalone application
Save always as Jpg (but accept gif and bmp too :P) Ok, we can live with that
Those bugs are easy to fix, eventually I'll do that, depending on my laziness done!
If you are interested in how is done, continue reading:
How to do a ftp droplet and applet that resizes images
The best(lazy) way to achieve the upload of large files and folders is to use ftp. For this I've used org.apache.commons.net.ftp classes. They are simple and well documented.
To start, see the FtpUploader class.
It basically iterate the files and folders specified in the swing tree component and upload them through the doUploadFile member
-
this.ftp.connect(FtpParameters.getString("FtpUploader.host"));
-
... //Check if connected and authenticate
-
-
//Get all the files in the model:
-
DefaultMutableTreeNode node;
-
while(nodesEnum.hasMoreElements()) {
-
this.doUploadFile(file);
-
}
doUploadFile upload files from a stream, in this way we can monitor the progress:
-
this.ftp.setFileType(FTP.IMAGE_FILE_TYPE);
-
... //Write and monitor progress
The resizeImage method of the JpgResizer class do what his name says:
-
//Wait for the image to load:
-
mediaTracker.addImage(image, 0);
-
mediaTracker.waitForID(0);
-
-
... //Calculate thumb size
-
... //Resize image
-
-
//Save as jpg:
-
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
-
... //Set jpg quality
-
encoder.encode(thumbImage);
-
out.close();
-
For the drag&drop code see DropFilesHandler class, in the drop method you can find the code that get the file name dropped.
Simple, isn't it?
To run it as an applet, you'll have to sign it. This is a good guide.
In short:
-
keytool.exe -genkey -storepass %jarsignerpassword -keyalg DSA -alias madarcoftpapplet -dname "CN=madarco.net, OU=Java Code, O=Madarco, L=Italy, ST=Italy, C=IT, MAILADDRESS=ftpapplet@madarco.it DC=ftpapplet, DC=com" -validity 999
-
-
keytool.exe -export -storepass %madapass -alias madarcoftpapplet -rfc -file ftpapplet.cer
-
-
jarsigner ftpApplet.jar madarcoftpapplet
The code is really quick&dirty, however, even if there are many things that can be improved, this can be used as a base to implement your own droplet.
July 20th, 2007 at 11:47 am
hello,
where can i find the html example?
thx
greetz
July 31st, 2007 at 6:47 pm
Sorry, here it is:
August 26th, 2007 at 10:15 pm
Your droplet look great
Just what I'm looking for.
Ik can't find the html example, and the applet above isn't loading.
Can you maybe make a zip containing the jar and the html?
Thanks in advance!
Jorre
August 27th, 2007 at 10:50 am
Sorry, I've pasted the code without the
tag
October 2nd, 2007 at 3:38 am
Hi, is it possible for you to leave me a java program that uploads folders to ftp using the org.apache.commons.net.ftp classes. I am having a proram that only upload files to http://ftp.
November 26th, 2007 at 7:52 pm
If have tested the original unmodified ftpApplet.jar with a sample.html file, but drag&drop doesn't work (Java 6 Update 3). Is there any trick? Or an online demo?
Thank you!
November 27th, 2007 at 8:18 pm
Sorry, I haven't an online demo.
Hewever, I've tried it and it seems to work with Java Plug-in 1.6.0_03.
Beware that it accept only jpg files.
You should also check if it is correctly signed.
Nb. I've found that in the web application "net2ftp" there is a good upload applet, but I haven't found the sources, maybe you'll be lukier.
January 4th, 2008 at 4:32 pm
Sorry, but you always need keytool.exe to run it as an applet? Can I run it as an application or without use keytool.exe. It seems a great applet. Thanks a lot for your help!
January 4th, 2008 at 5:12 pm
Yes, just run FtpDroplet class to run it as a standalone application.
You can even use some program (like JSmooth) to make an .exe
Ps. There was an error in the MANIFEST.MF of the jar, now I've fixed it, so if you re-download it you can just double-click on the jar to run it.
February 29th, 2008 at 3:07 pm
Hi! I started it as a standalone application, and it works perfectly. Can upload pictures to my domain. The only problem is that it doesn't seem to work with folders. If I drop a folder it just creates a folder on my domain, but the pictures in the folder is not uploaded.
Running Java 1.6.0_03 on Windows Vista
February 29th, 2008 at 5:15 pm
Hi,
For the folder problem, I'll investigate further, it seems to work for me.
In the mean time, can you check the permissions on the created folder?
February 29th, 2008 at 5:41 pm
Folder permissions: 755
The folder doesn't appear as in your screenshot. It's just a black dot in front of the folder name. There is no files underneath it either, so I guess the dropping is the problem in some way...
March 4th, 2008 at 10:50 am
Thanks to Johannes for the "uppercase extensions" bug