Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Scout/HowTo/4.0/Use RemoteFileService"

< Scout‎ | HowTo‎ | 4.0
(Replaced content with "The Scout documentation has been moved to https://eclipsescout.github.io/.")
 
Line 1: Line 1:
{{ScoutPage|cat=HowTo 4.0}}
+
The Scout documentation has been moved to https://eclipsescout.github.io/.
With RemoteFileService you can load files from the server to the client.
+
 
+
=== Set rootPath ===
+
 
+
Before using the remote file service, for security reasons, the server has to know what the root directory is where to load the files from. You can configure this setting in the config.ini of the server. If this parameter has not been set, you will get a SecurityException("invalid path for file service: path may not be null");
+
 
+
org.eclipse.scout.rt.server.services.common.file.RemoteFileService#rootPath=D:/
+
 
+
=== Get one file ===
+
On the client:
+
<source lang="java">
+
RemoteFile file = SERVICES.getService(IRemoteFileService.class).getRemoteFile(new RemoteFile("mydocuments", "document1.txt", 0L));
+
try {
+
  System.out.println(file.getName());
+
  System.out.println(new String(file.extractData()));
+
}
+
catch (IOException e) {
+
  e.printStackTrace();
+
}
+
</source>
+
 
+
It will print the name and the contents of the file.
+
 
+
=== Get all the files in a directory ===
+
<source lang="java">
+
RemoteFile[] files = SERVICES.getService(IRemoteFileService.class).getRemoteFiles("mydocuments", new NoFilenameFilter(), null);
+
for (int i = 0; i < files.length; i++) {
+
  System.out.println(files[i].getName());
+
  try {
+
    System.out.println(new String(files[i].extractData()));
+
  }
+
  catch (IOException e) {
+
    e.printStackTrace();
+
  }
+
}
+
</source>
+
the second parameter in the call is a instance of java.io.FilenameFilter
+
 
+
<source lang="java">
+
import java.io.File;
+
import java.io.FilenameFilter;
+
import java.io.Serializable;
+
+
public class NoFilenameFilter implements FilenameFilter, Serializable {
+
  @Override
+
  public boolean accept(File dir, String name) {
+
    return true;
+
  }
+
}
+
</source>
+
 
+
Create such a class and put it in the shared plug-in. Because this instance of FilenameFilter does not filter at all, the code above will print all the filenames. Be aware that it also loads all the contents of the files, so getting a lot of files will result in an out of memory soon.
+
 
+
=== Get particular files in a directory ===
+
If you create a better filter you can get only the files that you need.
+
 
+
<source lang="java">
+
import java.io.File;
+
import java.io.FilenameFilter;
+
import java.io.Serializable;
+
+
public class OnlyTxtFilenameFilter implements Serializable, FilenameFilter {
+
  @Override
+
  public boolean accept(File dir, String name) {
+
    return name.endsWith(".txt");
+
  }
+
}
+
</source>
+
 
+
Of course you could make your filter more flexible by adding a parameter to the constructor or create some setters.
+
 
+
=== Getting large files ===
+
If you need to get a large file, the RemoteFileService offers methods to only get a header (getRemoteFileHeader()) and to get the contents of the file in parts (getRemoteFilePart). See {{ScoutJavadoc|IRemoteFileService|I}} for detailed information.
+

Latest revision as of 07:35, 18 March 2024

The Scout documentation has been moved to https://eclipsescout.github.io/.

Back to the top