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:
ftpApplet

Download applet/droplet + sources

To use it:

  1. Download the jar+html example
  2. open sample.html
  3. Drag image files and folders of pictures
  4. Upload

In netmadarcolightviewUploadAppletFtpParameters.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:
DefaultTreeModel model = (DefaultTreeModel)this.applet.jPhotosTree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
Enumeration nodesEnum = root.children();
DefaultMutableTreeNode node;
while(nodesEnum.hasMoreElements()) {
    this.doUploadFile(file);
}

doUploadFile upload files from a stream, in this way we can monitor the progress:

BufferedInputStream imgStream = this.resizer.resizeImage(file.getAbsolutePath());
this.ftp.setFileType(FTP.IMAGE_FILE_TYPE);
OutputStream outputStream = this.ftp.storeFileStream(file.getName());
... //Write and monitor progress

The resizeImage method of the JpgResizer class do what his name says:

Image image = Toolkit.getDefaultToolkit().getImage(imageFile);
//Wait for the image to load:
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);

... //Calculate thumb size
... //Resize image

//Save as jpg:
File file = File.createTempFile("uploadApplet", "img");
BufferedOutputStream out = new BufferedOutputStream(new
	    	      FileOutputStream(file));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
... //Set jpg quality
encoder.encode(thumbImage);
out.close();

return new BufferedInputStream(new FileInputStream(file));

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.

15 thoughts on “Simple java ftp upload and resize pics droplet & applet

  1. Madarco says:

    Sorry, here it is:

    <HTML>
    <HEAD>
    <TITLE>Upload Applet</TITLE>
    </HEAD>
    <BODY>
    <applet code=net.madarco.lightview.UploadApplet.FtpApplet.class archive="ftpApplet.jar" width="100%" height="100%" border="0">
    </applet>
    </BODY>
    </HTML>
    
  2. jorre says:

    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

  3. Vijay says:

    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 ftp.

  4. Max says:

    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!

  5. Madarco says:

    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.

  6. Carlos Saric says:

    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!

  7. Madarco says:

    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.

  8. Johannes says:

    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

  9. Madarco says:

    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?

  10. Johannes says:

    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…

  11. Madarco says:

    Have you tried to look at the java console?
    You can usually find it in you browser Tools menu.
    If there is some messages post it here and I’ll try to fix it 🙂

Comments are closed.