Saturday, June 14, 2008

Java filename generation

I have a Java web application that takes user inputted values and makes a nicely formatted png image for the column heading.

I ran into an interesting problem today that I thought would be simple to solve but was rather suprised that it wasn't.

For performance reasons when I generated this png I cache it to disk, out of laziness I simply stored it with the filename the user gave. Obviously now the app is beginning to be used the user decided to enter 'MyName (8/4/8)' and my app barfed because the '/' is a special character.

This was somewhat expected and I decided to fix it real quick but was surprised that there was no way to easily escape this input.

In the end i ended up Base64 encoding the value then writing it as a string. In other words using a simple hash as the name.

So I pose the question, is there a better way to do this? Will the Base64 always return valid file name?

--
C

No comments: