Skip to main content
1-Visitor
July 27, 2016
Solved

Transfer file from TW Server to remote JavaSDK

  • July 27, 2016
  • 1 reply
  • 5921 views

Hi Experts, Coluld you pls guide me on steps for

Transfer a file(zip) from TW Server to remote JavaSDK

I tried something, but not succeeded, using Copy service.

With that I cud achieve only copy a file between two folders, both on the TW Server.

My need is to copy a file from TW Server to remote JavaSDK.

Thanks

Best answer by mhollenbach

Sujith,

You are correct in using the Copy service, but there are a couple of things you need to make sure are configured correctly:

  • A FileRepository Thing has been created on the ThingWorx server - in the following code sample (from a custom service) I've named my FileRepository "TestingRepo"
    • sourceRepo is the name of my FileRepository in ThingWorx
    • targetRepo is the name of the Edge Thing defined in the Java SDK

var params = {

  async: false /* BOOLEAN */,

  sourceRepo: "TestingRepo" /* STRING */,

  targetRepo: "FileTransferExample" /* STRING */,

  targetFile: "werd_2014.gif" /* STRING */,

  targetPath: "in" /* STRING */,

  sourceFile: "werd_2014.gif" /* STRING */,

  sourcePath: "Meghan" /* STRING */,

  timeout: undefined /* INTEGER */

};

// result: INFOTABLE dataShape: FileTransferJob

var result = Subsystems["FileTransferSubsystem"].Copy(params);


  • The following is the entire Java file (provided in the Java SDK download) that sets up Virtual Directories for a file transfer.
    • If you look at the custom service above to see that we are referencing the targetPath for a directory that is defined in the Java class.


package com.thingworx.javatransfer;

import com.thingworx.communications.client.ClientConfigurator;

import com.thingworx.communications.client.ConnectedThingClient;

import com.thingworx.communications.client.things.filetransfer.FileTransferVirtualThing;

public class FileTransferExample {

  // Substitute your thing name here

  private static final String ThingName = "FileTransferExample";

  public static void main(String[] args) {

  // Create a client config

  ClientConfigurator config = new ClientConfigurator();

  // Basic configuration. See SimpleClient.java for additional info.

  config.setUri("wss://localhost:8443/Thingworx/WS");

  config.setAppKey("8737c3b9-bc65-4ac8-8990-64b565487cd3");

  config.ignoreSSLErrors(true);

  try {

  ConnectedThingClient client = new ConnectedThingClient(config);

  FileTransferVirtualThing myThing = new FileTransferVirtualThing(ThingName, "File Transfer Example", client);

  // Add two virtual directories that will act as the root directories in this

  // application's virtual file system.

  myThing.addVirtualDirectory("in",  "/home/meg/files/in");

  myThing.addVirtualDirectory("out", "/home/meg/files/out");

  client.bindThing(myThing);

  client.start();

  while(!client.isShutdown()) {

  Thread.sleep(5000);

  }

  } catch(Exception e) {

  e.printStackTrace();

  }

  }

}

Let me know if you have any further questions.

Meghan

1 reply

5-Regular Member
July 27, 2016

Sujith,

You are correct in using the Copy service, but there are a couple of things you need to make sure are configured correctly:

  • A FileRepository Thing has been created on the ThingWorx server - in the following code sample (from a custom service) I've named my FileRepository "TestingRepo"
    • sourceRepo is the name of my FileRepository in ThingWorx
    • targetRepo is the name of the Edge Thing defined in the Java SDK

var params = {

  async: false /* BOOLEAN */,

  sourceRepo: "TestingRepo" /* STRING */,

  targetRepo: "FileTransferExample" /* STRING */,

  targetFile: "werd_2014.gif" /* STRING */,

  targetPath: "in" /* STRING */,

  sourceFile: "werd_2014.gif" /* STRING */,

  sourcePath: "Meghan" /* STRING */,

  timeout: undefined /* INTEGER */

};

// result: INFOTABLE dataShape: FileTransferJob

var result = Subsystems["FileTransferSubsystem"].Copy(params);


  • The following is the entire Java file (provided in the Java SDK download) that sets up Virtual Directories for a file transfer.
    • If you look at the custom service above to see that we are referencing the targetPath for a directory that is defined in the Java class.


package com.thingworx.javatransfer;

import com.thingworx.communications.client.ClientConfigurator;

import com.thingworx.communications.client.ConnectedThingClient;

import com.thingworx.communications.client.things.filetransfer.FileTransferVirtualThing;

public class FileTransferExample {

  // Substitute your thing name here

  private static final String ThingName = "FileTransferExample";

  public static void main(String[] args) {

  // Create a client config

  ClientConfigurator config = new ClientConfigurator();

  // Basic configuration. See SimpleClient.java for additional info.

  config.setUri("wss://localhost:8443/Thingworx/WS");

  config.setAppKey("8737c3b9-bc65-4ac8-8990-64b565487cd3");

  config.ignoreSSLErrors(true);

  try {

  ConnectedThingClient client = new ConnectedThingClient(config);

  FileTransferVirtualThing myThing = new FileTransferVirtualThing(ThingName, "File Transfer Example", client);

  // Add two virtual directories that will act as the root directories in this

  // application's virtual file system.

  myThing.addVirtualDirectory("in",  "/home/meg/files/in");

  myThing.addVirtualDirectory("out", "/home/meg/files/out");

  client.bindThing(myThing);

  client.start();

  while(!client.isShutdown()) {

  Thread.sleep(5000);

  }

  } catch(Exception e) {

  e.printStackTrace();

  }

  }

}

Let me know if you have any further questions.

Meghan

ses1-VisitorAuthor
1-Visitor
July 28, 2016

A million Thanks, Meghan.

The description from your perfectly answers my question.

Yea, the code I had tried was similar to what you have provided.

But the problem was there at how I configured params.

Now it is perfectly fine, Thanks again !!!

1-Visitor
October 13, 2016

Sujith, is there any chance that you could share me your knowledge about sending files to Thingworx? I'm stucked trying to send a picture from a smartphone to a Thingworx platform and I have no idea on how to implement FileTransfer with the Android SDK.

I'll apreciate your help.