Why is it so complicated with Java to access files on a remote Java Servlet container ?

For example, why is it so complicated to code the upload of a file from an Android device or a PC Desktop to a remote Tomcat or other JavaEE server for treatment by a Servlet? Java developers have no easy built in APIs or tools to handle this. There is the solution to set up a FTP server but itís cumbersome: it requires amendments to security rules and new developments to link the uploaded file to Tomcat or other JavaEE Servlet Server. Another solution is to use client and server libraries (Apache Commons HttpClient, FileUpload, etc.), but you have to write, test and maintain the code, define the security rules, handle proxy considerations, manage the errors, etc.

Awake FILE is our attempt to offer an easy solution for:

Code Samples

File upload through HTTP

This snippet shows how to upload a file with few lines of code:

  // Define URL of the path to the Server File Manager servlet
  String url = "https://www.acme.org/ServerFileManager";

  // The login info for strong authentication on server side:
  String username = "myUsername";
  char[] password = 'm''y''P''a''s''s''w''o''r''d' };

  // Establish a session with the remote server
  RemoteSession remoteSession = new RemoteSession(url, username, password);

  // Upload a file using built-in RemoteSession.upload()
  File file = new File("C:\\Users\\Mike\\Koala.jpg");
  String remotePath = "/Koala.jpg";

  remoteSession.upload(file, remotePath);

There is no programming on the server side (except few lines for files location and security settings if required, see the Tutorial).

Same file upload using a RemoteOutputStream. Allows to feed a GUI Progress Bar (not coded here):

  // Establish a session with the remote server
  RemoteSession remoteSession = new RemoteSession(url, username, password);

  // Upload a file using a RemoteOutputStream
  InputStream in = null;
  OutputStream out = null;
  
  File localFile = new File("C:\\Users\\Mike\\Koala.jpg");
  String remotePath = "/Koala.jpg";

  try {
      // Get an InputStream from our local file
      in = new FileInputStream(localFile);

      // Create an OutputStream that maps a remote file on the host
      out = new RemoteOutputStream(remoteSession, remotePath,
        localFile.length());

      // Create the remote file reading the InpuStream and writing
      // on the OutputStream
      byte[] buffer = new byte[1024 4];
      int n = 0;
      while ((n = in.read(buffer)) != -1) {
          out.write(buffer, 0, n);
      }
  finally {
      if (in != nullin.close();
      if (out != nullout.close();
  }

A complete example that increments a Progress Bar in the read/write loop is available in: FileTransferProgressMonitorDemo.java.

Accessing remote files and directories

If you know to use java.io.File, you know how to use RemoteFile to access remote files and directories:

  // Establish a session with the remote server
  RemoteSession remoteSession = new RemoteSession(url, username, password);

  // Create a new RemoteFile that maps a file on remote server
  RemoteFile remoteFile = new RemoteFile(remoteSession, "/Koala.jpg");

  // RemoteFile methods have the same names, signatures and behaviors
  // as java.io.File methods: a RemoteFile method is a File method that
  // is executed on the remote host
  if (remoteFile.exists()) {
      System.out.println(remoteFile.getName() " length  : "
        + remoteFile.length());
      System.out.println(remoteFile.getName() " canWrite: "
        + remoteFile.canWrite());
  }

  // List files on our remote root directory
  remoteFile = new RemoteFile(remoteSession, "/");

  RemoteFile[] files = remoteFile.listFiles();
  for (RemoteFile file : files) {
      System.out.println("Remote file: " + file);
  }

  // List all text files in out root directory
  // using an Apache Commons IO 2.4 FileFiter
  FileFilter fileFilter = new SuffixFileFilter(".txt");

  files = remoteFile.listFiles(fileFilter);
  for (RemoteFile file : files) {
      System.out.println("Remote text file: " + file);
  }
  
  // Create a new remote directory
  new RemoteFile(remoteSession, "/my_new_dir").mkdirs();

Remote Java method call

This snippet shows how to call a remote Java method:

  // Establish a session with the remote server
  RemoteSession remoteSession = new RemoteSession(url, username, password);

  // OK: call the add(int a, int b) remote method that returns a + b:
  String result = remoteSession.call(
    "org.kawanfw.examples.Calculator.add"3344);
  System.out.println("Calculator Result: " + result);