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
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

Curious about the future of data-driven systems? Join our Data Engineering roundtable and learn how to build scalable data platforms.

Data Engineering: The industry has come a long way from organizing unstructured data to adopting today's modern data pipelines. See how.

Threat Detection: Learn core practices for managing security risks and vulnerabilities in your organization — don't regret those threats!

Managing API integrations: Assess your use case and needs — plus learn patterns for the design, build, and maintenance of your integrations.

Related

  • Oracle: Migrate PDB to Another Database
  • Remote Control With Node.js, React.js, and Raspberry Pi
  • How To Protect RDP From Ransomware Attacks
  • Remote Video Security Surveillance

Trending

  • Understanding the Differences Between Repository and Data Access Object (DAO)
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • AWS Redshift Data Sharing: Unlocking the Power of Collaborative Analytics
  • Increase Model Flexibility and ROI for GenAI App Delivery With Kubernetes

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
13.7K 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

  • Oracle: Migrate PDB to Another Database
  • Remote Control With Node.js, React.js, and Raspberry Pi
  • How To Protect RDP From Ransomware Attacks
  • Remote Video Security Surveillance

Partner Resources


Comments

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: