Simple java ftp upload and resize pics droplet & applet
Saturday, May 26th, 2007Update3 (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.