This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Writing/Upload file to 2 servers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Writing/Upload file to 2 servers" Watch "Writing/Upload file to 2 servers" New topic
Author

Writing/Upload file to 2 servers

Pan Igor
Greenhorn

Joined: Aug 18, 2009
Posts: 4

I have 2 servers that are load balanced. When I upload a file, it is uploaded only to the server that processes the request. I want to upload a file to both the servers. Is that possible? If so, how?

See simple code below to write the file.


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

What we did when we had a similar requirement was not to store the files on either of the load-balanced server, but to store them on a completely separate drive. Then we configured the two servers to access that drive for shared files.
Pan Igor
Greenhorn

Joined: Aug 18, 2009
Posts: 4
I can not use another server to store files. Any other way? How can I store files using server names? java.io.File does not have the API
Timothy Frey
Ranch Hand

Joined: Jul 22, 2006
Posts: 56
We faced a similar problem. The first thing we did was hide all File I/O behind an interface that had 2 methods: save and load. It's important that none of your code accesses the filesystem directly because you want this kind of file replication to be handled consistently.

The first implementation of this service will probably just do whatever you are doing now, namely just saving or reading from local files. This step is basically just a refactoring of your existing code.

The next step is to write another implementation that will handle the file replication for you. In our code, the save method did the following:

1) Save the data to the local file system.
2) Write the file's checksum into a database somewhere (I'll explain later).
3) Attempt to copy (in parallel) the file to other servers in the cluster. We defined which servers belonged in each cluster through a property file.

On Windows, you can access another computer's files by doing something like: \\remote_machine\folder1\file.txt or something like that. Not entirely sure how to do it on Linux but I imagine it can't be too hard.

Finally, whenever a piece of the code needed to read a file, it calls the load method. This method first checks the local file system. If the file's there, great, if not, attempt to find it on one of the other servers in the cluster. We used the checksum here to make sure the file is valid; it's possible it might be corrupted. If it's corrupted, we move on to the next server. If no valid copy of the file exists, you can throw a FileNotFoundException or something similar.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing/Upload file to 2 servers
 
Similar Threads
JSP uploader
How to get upload an image on server and get it on next jsp?
Image Uploading Problem In Tomcat Live Server
Commons FileUpload
exception in uploading the file