Archive for May, 2007

Simple java ftp upload and resize pics droplet & applet

Saturday, May 26th, 2007

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

My two cents on Static Typing

Wednesday, May 16th, 2007

While I was wondering if using haXe or Java with my server, I've stumbled upon some discussion on Static Typing (Java) vs. Dynamic Typing (Ruby, Php).
In a really interesting blog post (read the comments too) I found this touching phrase:

"Static typing is Communist Bureaucracy".

In short: since we (should) have pervasive testing, Static Typing is only more testing. However, a comment is enlighting:

I'm finding this logic hard to follow. Testing is good, coverage is good. Static typing is, among other things, a way to have the compiler test your code for type issues. Since we like testing, this makes static typing good, right?

But Static Typing not only prevent you to do mistakes, like typos, it allows your IDE to know what you are doing:

Dynamic Static Typing speeds up development because there is physically less to type since ctrl+space and ctrl+2 write the declarations for you.

Its seems obvious to me that Dynamic Typing is worth the catch only for a quick start or for learning.
Since almost all of the dynamically typed languages (like Actionscript, Perl, Php) have migrated to some kind of Static Typing (eg: Type Inference), there should be a reason.

Periodically, a new Dynamically Typed language spawn, attracting junior/tired programmers with the mirage of the simplicity and the less-to-type-ing, riding the hype until his young audience grows and start doing large projects hoping not to call a function with the wrong arguments.

Only then they realize the big truth: Static Typing is just more informations in the source code.
And more information means less wondering-what-object-was or what-was-the-method-name-and-parameters or who-instantiate-this-object, not to mention static analysis.

And hey, they have invented Javadoc-like documentation and Annotations just to add some more information, why remove one of the most important?

10 Great Muppet Moments in Muppet History

Thursday, May 10th, 2007

Just like the title, incredibly fun.

10 Great Muppet Moments in Muppet History

Flyer - Python framework for Flash Lite

Tuesday, May 8th, 2007

Flyer is a open source framwork for symbian phones written in python.

From the site:

Flyer is based on the Flash Lite 2.1 integration with Python for S60 and contains the following features:

- Take photos with device camera from Flash Lite
- Take device screenshots from Flash Lite
- Send files over a bluetooth connection from Flash Lite
- Get the contact entries in the default device database
- Text To Speech
- Sound recording and playback

Moreover: 60% of all donations to Flyer Project will be for Brazilian Children

MindFrame javascript templating+framework

Sunday, May 6th, 2007

The Javascript's world is spinning faster in the last times. An interesting framework was beta-released today, called MindFrame, it allows to define complex behaviors directly in the HTML source using a special syntax.
The parser used is Zparse.

You have to see the syntax in the demo to understand how cool it is.

The idea of parsing an HTML element as the source of a template language in Javascript to produce a RIA really blow my mind off, may be the sign of Javascript surpassing Flash?

Some thougths on Actionscript in the serverside

Friday, May 4th, 2007

I've been always fascinated by the capabilities of Flash, especially in making networked applications (and games) that can run everywhere.

Flash is the only tool that can bring to the maximum audience with the minimum effort a full fledged application/media player/real time network client.
Yes, low-latency, bi-directional, real time messaging. (using XmlSocket connections)

But, even if Flash is such amazing stuff, why there isn't so much real time, synchronized, networked Flash applications out there?

After some thoughts, some non-blocking servers and a thesis (jira), I think I've found the answer: the lack of a server.

Yes, there are many, like the promising Red5, the full-fledged (non-free) SmartFox, Oregano, and others, but no one seems to reach a decent level of notoriety and support.

The chosen one seems to be Red5, with SmartFox coming next with his maturity but the lack of streaming, but there is another one that can won the race with a large margin: haXe.

For those who doesn't know, haXe is a compiler/language written by the authors of MTASC (Motion-Twin). His main feature is that it can output running code (or bytecode) for Javascript, Flash 8-9 and the Neko VM, with the same language.
But the most interesting features are coming form the Neko VM: the ability to run as a Apache module and the non-blocking sockets support.

What does it mean?

This mean that with only one language and a common set of libraries it is possible not only to build the client, but even the server side of a web application.
And for server side, I mean all kind of server: 'classic' webserver serving html from a database, Flash Remoting or a massive chat/multiplayer non-blocking server.

Whats the catch? It's a C/C++ stuff and the lack of a large community/hype. However, the capabilities are already exciting.

I hope to find the time to take a better look to haXe. In the meantime, I'll monitor the Java scripting languages support waiting for the ActionScript 3 (Tamarin) ScriptEngine.

Flex 2 components in papervision

Friday, May 4th, 2007

Yes, its really wonderful: fully working Flex 2 components in a 3D enviroment (via Gianfranco Torlone)