DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Impact of Asynchronous Work on Engineering Innovation
  • Oracle: Migrate PDB to Another Database
  • Remote Control With Node.js, React.js, and Raspberry Pi
  • How To Protect RDP From Ransomware Attacks

Trending

  • Why We Still Struggle With Manual Test Execution in 2025
  • Evolution of Cloud Services for MCP/A2A Protocols in AI Agents
  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases
  • Java Virtual Threads and Scaling

Awake File 3.0: access remote files securely using java.io.File APIs

By 
Nicolas De Pomereu user avatar
Nicolas De Pomereu
·
Apr. 08, 15 · News
Likes (0)
Comment
Save
Tweet
Share
14.1K Views

Join the DZone community and get the full member experience.

Join For Free

Awake FILE is a secure Open Source framework which enables Android and Java Desktop developers to very easily implement file uploads/downloads, RPC and remote file access through HTTP. File transfers include powerful features like file chunking and automatic recovery mechanism.

The new 3.0 version include zero-learning APIs to access remote files, upload or download files (see examples below).

Awake FILE is licensed through the GNU Lesser General Public License (LGPL v2.1): you can use it for free and without any constraints in your open source projects as well as in your commercial applications.

Why Awake FILE?

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:

  • Uploading files to a remote servlet server.
  • Downloading files from a remote servlet server.
  • Creating/deleting/accessing remote files and directories.
  • Calling a remote java method without complicated setup.
  • Defining strong security rules for all these operations.

v3.0 APis: Code Examples

Session Establishment

RemoteSession is the only API class you’ll have to learn. It is used to establish a session with the remote server:

 // 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);

The client side talks to the server via a servlet named Server File Manager.

There is no complicated setup, “registration” or programming on the server side (except few lines of code for files location and security settings if required.)

Uploading a File

Just use RemoteOutputStream that implements OutputStream:

 // 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 {
      in = new FileInputStream(localFile);

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

      // Copy the bytes to create our remote file
      byte[] buffer = new byte[1024 * 4];
      int n = 0;
      while ((n = in.read(buffer)) != -1) {
          out.write(buffer, 0, n);
      }
  } finally {
      if (in != null) in.close();
      if (out != null) out.close();
  }


Downloading a Remote File

Just use RemoteInputStream that implements InputStream:

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

	File file = new File("C:\\Users\\Mike\\Koala_DOWNLOADED.jpg");
	String remotePath = "/Koala.jpg";

	InputStream in = null;
	OutputStream out = null;

	try {

	    // Get an InputStream from the file located on our server
	    in = new RemoteInputStream(remoteSession, remotePath);
	    
	    out = new FileOutputStream(file);

	    // Copy the remote bytes to create our local file
	    byte[] buffer = new byte[1024 * 4];
	    int n = 0;
	    while ((n = in.read(buffer)) != -1) {
		        out.write(buffer, 0, n);
	    }

	} finally {
	    if (in != null)in.close();
	    if (out != null) out.close();
	}

Awake File includes Apache Commons IO. We can simplify the 2 examples (if we don’t need to setup a GUI Progress Bar).

Example for file upload:

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

	File file = new File("C:\\Users\\Mike\\Koala.jpg");
	String remotePath= "/Koala.jpg";

	InputStream in = null;
	OutputStream out = null;

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

	    // upload file
	    IOUtils.copy(in, out);

	} finally {
	    IOUtils.closeQuietly(in);
	    IOUtils.closeQuietly(out);
	}


Accessing Remote Files

Awake FILE 3.0 gives now the power to access remote files using well known java.io.File methods. 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. There is no setup or “registration” on server side:

  // 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", 33, 44);
  System.out.println("Calculator Result: " + result);


Documentation, Tutorial, Source Code & Binaries

Please visit http://www.awake-file.org.

We will be very happy to have your comments and reviews!


remote

Opinions expressed by DZone contributors are their own.

Related

  • The Impact of Asynchronous Work on Engineering Innovation
  • Oracle: Migrate PDB to Another Database
  • Remote Control With Node.js, React.js, and Raspberry Pi
  • How To Protect RDP From Ransomware Attacks

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!