The Wayback Machine - https://web.archive.org/web/20070702075756/http://blog.madarco.net:80/73/simple-java-ftp-upload-and-resize-pics-droplet-applet/

Simple java ftp upload and resize pics droplet & applet

Posted in Java at 5:04 pm by Madarco

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

JAVA:

  1. this.ftp.connect(FtpParameters.getString("FtpUploader.host"));
  2. ... //Check if connected and authenticate
  3.  
  4. //Get all the files in the model:
  5. DefaultTreeModel model = (DefaultTreeModel)this.applet.jPhotosTree.getModel();
  6. Enumeration nodesEnum = root.children();
  7. while(nodesEnum.hasMoreElements()) {
  8.     this.doUploadFile(file);
  9. }

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

JAVA:

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

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

JAVA:

  1. Image image = Toolkit.getDefaultToolkit().getImage(imageFile);
  2. //Wait for the image to load:
  3. MediaTracker mediaTracker = new MediaTracker(new Container());
  4. mediaTracker.addImage(image, 0);
  5. mediaTracker.waitForID(0);
  6.  
  7. ... //Calculate thumb size
  8. ... //Resize image
  9.  
  10. //Save as jpg:
  11. File file = File.createTempFile("uploadApplet", "img");
  12.                   FileOutputStream(file));
  13. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  14. ... //Set jpg quality
  15. encoder.encode(thumbImage);
  16. out.close();
  17.  
  18. 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:

CODE:

  1. 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
  2.  
  3. keytool.exe -export  -storepass %madapass -alias madarcoftpapplet -rfc -file ftpapplet.cer
  4.  
  5. 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.

Leave a Reply

Antispam: