Saving attachments

A system we are developing has as a requirement the saving of file attachments. We are currently saving these as BLOBs in our MySQL database, which works well. However the database is growing in size very quickly, so we are considering saving the files in a standardised directory structure on the server. This would allow us to carry out smaller incremental backups.

How can we go about this? We do not want to give all clients access to the drive on the server, so the saving must be done through the server somehow. We thought of saving the attachment as a BLOB to a temporary database table and having a server side script periodically look at the table, extracting anything it found and writing it to the server’s disk. However, this seems rather complicated.

Does anyone have any other ideas?

Well, if you don’t give the client access to the network drive you won’t be able to read the file back if you stored it using a server side script. What about having a separate MySQL database that can grow?

This could probably be done fairly easily with the module sdk, and probably in several different ways. The easiest thing to do would probably be to implement two components - one for uploading, and one for downloading, and use the module rpc functionality to communicate from these components to the module’s counterpart on the gateway.

Of course, a example would sure help illustrate this… if you’re interested in this route, I’ll see if I can [get someone to] whip something up.


I agree with Colby - writing a module that simply added a servlet to the Gateway that piped back files from a location on the server would be trivial.

You would be able to access them via a url like:

That sounds like a really useful module!

FTP accounts is what I use here. you can set up the account to write files only, then use the web server to dish them out as needed.

We still have this requirement, in fact it’s more urgent than ever due to the now huge size of the database!

Colby, you suggested you may be able to get someone to create an example of this - is this offer still open?


Sure, I’ll see what I can do.


Hey, sorry for the delay. I haven’t forgotten about this, but I haven’t had time to put anything together.

Since I think you guys are pretty good at putting together modules, I thought I would just give you some high level ideas:

First, a quick overview:

  1. On the gateway side, your module would use the getRPCHandler to return an RPC class that would have a function such as:
public void storeFile(String name, byte[] data)
  1. On the client side, you would have a component that would allow file selection, and then would call that RPC function against the gateway.

So, for part #2, I’ve attached an example of a class that does this. The example is only a little different, in that it expects the file to be an image, and shows a preview of it, but you can just get rid of the preview part.

For part #1, here is a potential implementation:

public void storeFile(String name, byte[] data) throws Exception { String path = "tomcat/webapps/main/"+ name; FileUtils.writeByteArrayToFile(new File(path), data); }

And that’s it. Note that I’m using Apache Commons’ FileUtils class, which is available to all modules. There are many different things you could do, but the path I’ve chosen to use here will make the file immediately available from the gateway under: “http://localhost:8088/main/filename”. I’m sure there are various cool things you could do to put it elsewhere, using tomcat config, but at least with this mechanism, you don’t need to change anything on a new install.

Hope this helps a bit, let me know if you have any questions. (3.53 KB)

Thanks Colby, we’ll give that a try.