The content included here should be enough to get started, but please visit our Developer Documentation Website for the complete documentation.
The Files.com Java client library provides convenient access to the Files.com API from JVM based applications.
A maven jar is available through maven-central.
To use the package add the following to your pom.xml
file.
<dependency>
<groupId>com.files</groupId>
<artifactId>files-sdk</artifactId>
</dependency>
To add the dependency to your Gradle project add this to your
compile group: 'com.files', name: 'files-sdk'
The Files.com Java SDK supports all versions of Java beginning with Java 8 (Also known as 1.8).
Explore the [files-sdk-java](https://github.com/Files-com/files-sdk-java) code on GitHub.The Files.com team is happy to help with any SDK Integration challenges you may face.
Just email support@files.com and we'll get the process started.
Authenticating with an API key is the recommended authentication method for most scenarios, and is the method used in the examples on this site.
To use the API or SDKs with an API Key, first generate an API key from the web interface or via the API or an SDK.
Note that when using a user-specific API key, if the user is an administrator, you will have full access to the entire API. If the user is not an administrator, you will only be able to access files that user can access, and no access will be granted to site administration functions in the API.
FilesClient.apiKey = "YOUR_API_KEY";
// Alternatively, you can specify the API key on a per-object
// basis in options HashMap to a model constructor.
HashMap<String, Object> requestOptions = new HashMap()<>;
requestOptions.put("api_key", "my-key");
User user = new User(params, requestParameters);
// You may also specify the API key on a per-request basis in
// in the final parameter to static methods.
HashMap<String, Object> requestOptions = new HashMap()<>;
requestOptions.put("api_key", "my-key");
User.find(id, params, requestOptions);
You can also authenticate to the REST API or SDKs by creating a user session using the username and password of an active user. If the user is an administrator, the session will have full access to the entire API. Sessions created from regular user accounts will only be able to access files that user can access, and no access will be granted to site administration functions.
API sessions use the exact same session timeout settings as web interface sessions. When an API session times out, simply create a new session and resume where you left off. This process is not automatically handled by SDKs because we do not want to store password information in memory without your explicit consent.
To create a session, the create
method is called on the Session
object with the user's username and
password.
This returns a session object that can be used to authenticate SDK method calls.
HashMap<String, Object> sessionParameters = new HashMap<>()
sessionParameters.put("username", "username");
sessionParameters.put("password", "password");
Session session = Session.create(parameters)
Once a session has been created, you can store the session globally, use the session per object, or use the session per request to authenticate SDK operations.
// You may set the returned session to be used by default for subsequent requests.
FilesClient.session = session;
// Alternatively, you can specify the session ID on a per-object basis
// in the second parameter to a model constructor.
HashMap<String, Object> requestParameters = new HashMap<>();
requestParameters.put("session_id", session.getId());
user = new User(params, requestParameters);
// You may also specify the session ID on a per-request basis in the final parameter to static methods.
HashMap<String, Object> requestParameters = new HashMap<>();
requestParameters.put("session_id", session.getId());
User.find(id, params, requestParameters);
User sessions can be ended calling the destroy
method on the session
object.
session.destroy()
Setting the base URL for the API is required if your site is configured to disable global acceleration. This can also be set to use a mock server in development or CI.
FilesClient.setProperty("apiRoot", "https://MY-SUBDOMAIN.files.com");
The Files.com SDK is compatible with the standard log4j logging scheme.
Add com.files
logger to your Loggers
root in the log4j2.xml
file.
<Loggers>
<!-- set preferred level -->
<Logger name="com.files" level="TRACE" />
<!-- to enable network request -->
<Logger name="okhttp3.logging.wire" level="INFO"/>
</Loggers>
Create a resources/log4j2.xml
file.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<!-- set preferred level -->
<Logger name="com.files" level="TRACE"/>
<!-- to enable network request -->
<Logger name="okhttp3.logging.wire" level="INFO"/>
</Loggers>
</Configuration>
You can read more about log4j2 configuration.
The Files.com Java SDK will return errors by raising exceptions. There are many exception classes defined in the Files SDK that correspond to specific errors.
The raised exceptions come from two categories:
- SDK Exceptions - errors that originate within the SDK
- API Exceptions - errors that occur due to the response from the Files.com API. These errors are grouped into common error types.
There are several types of exceptions within each category. Exception classes indicate different types of errors and are named in a
fashion that describe the general premise of the originating error. More details can be found in the exception object message using the
getMessage()
method.
Use standard Java exception handling to detect and deal with errors. It is generally recommended to catch specific errors first, then
catch the general SdkException
exception as a catch-all.
package com.filescom.app;
import java.util.HashMap;
import com.files.models.*;
import com.files.exceptions.*;
import com.files.exceptions.ApiErrorException.*;
public class App
{
public static void main( String[] args )
{
HashMap<String, Object> sessionParameters = new HashMap<>();
sessionParameters.put("username", "USERNAME");
sessionParameters.put("password", "BADPASSWORD");
try{
Session session = Session.create(sessionParameters);
}
catch(NotAuthenticatedException e){
System.out.println("Authentication Error Occrured (" + e.getClass().getName() +"): " + e.getMessage());
}
catch(SdkException e){
System.out.println("Unknown Error Occrured (" + e.getClass().getName() +"): " + e.getMessage());
}
System.out.println( "The End." );
}
}
SDK errors are general errors that occur within the SDK code. These errors generate exceptions. Each of these
exception classes inherit from a standard SdkException
base class.
com.files.exceptions.ApiErrorException.ApiConnectionException ->
com.files.exceptions.SdkException ->
RuntimeException
Exception Class Name | Description |
---|---|
ApiConnectionException |
The Files.com API cannot be reached |
AuthenticationException |
Authentication Failer on the Files.com API |
InvalidParameterException |
A passed in parameter is invalid |
InvalidResponseException |
A bad formed response came back from the API |
ServerErrorException |
The API service responded with a bad response (ie, 5xx) |
API errors are errors returned by the Files.com API. Each exception class inherits from an error group base class. The error group base class indicates a particular type of error.
com.files.exceptions.ApiErrorException.FolderAdminPermissionRequiredException ->
com.files.exceptions.ApiErrorException.NotAuthorizedException ->
com.files.exceptions.ApiErrorException ->
com.files.exceptions.SdkException ->
RuntimeException
Exception Class Name | Error Group |
---|---|
AgentUpgradeRequiredException |
BadRequestException |
AttachmentTooLargeException |
BadRequestException |
CannotDownloadDirectoryException |
BadRequestException |
CantMoveWithMultipleLocationsException |
BadRequestException |
DatetimeParseException |
BadRequestException |
DestinationSameException |
BadRequestException |
FolderMustNotBeAFileException |
BadRequestException |
InvalidBodyException |
BadRequestException |
InvalidCursorException |
BadRequestException |
InvalidCursorTypeForSortException |
BadRequestException |
InvalidEtagsException |
BadRequestException |
InvalidFilterAliasCombinationException |
BadRequestException |
InvalidFilterCombinationException |
BadRequestException |
InvalidFilterFieldException |
BadRequestException |
InvalidFilterParamException |
BadRequestException |
InvalidFilterParamValueException |
BadRequestException |
InvalidInputEncodingException |
BadRequestException |
InvalidInterfaceException |
BadRequestException |
InvalidOauthProviderException |
BadRequestException |
InvalidPathException |
BadRequestException |
InvalidReturnToUrlException |
BadRequestException |
InvalidUploadOffsetException |
BadRequestException |
InvalidUploadPartGapException |
BadRequestException |
InvalidUploadPartSizeException |
BadRequestException |
MethodNotAllowedException |
BadRequestException |
NoValidInputParamsException |
BadRequestException |
PartNumberTooLargeException |
BadRequestException |
PathCannotHaveTrailingWhitespaceException |
BadRequestException |
ReauthenticationNeededFieldsException |
BadRequestException |
RequestParamsContainInvalidCharacterException |
BadRequestException |
RequestParamsInvalidException |
BadRequestException |
RequestParamsRequiredException |
BadRequestException |
SearchAllOnChildPathException |
BadRequestException |
UnsupportedCurrencyException |
BadRequestException |
UnsupportedHttpResponseFormatException |
BadRequestException |
UnsupportedMediaTypeException |
BadRequestException |
UserIdInvalidException |
BadRequestException |
UserIdOnUserEndpointException |
BadRequestException |
UserRequiredException |
BadRequestException |
AdditionalAuthenticationRequiredException |
NotAuthenticatedException |
AuthenticationRequiredException |
NotAuthenticatedException |
BundleRegistrationCodeFailedException |
NotAuthenticatedException |
FilesAgentTokenFailedException |
NotAuthenticatedException |
InboxRegistrationCodeFailedException |
NotAuthenticatedException |
InvalidCredentialsException |
NotAuthenticatedException |
InvalidOauthException |
NotAuthenticatedException |
InvalidOrExpiredCodeException |
NotAuthenticatedException |
InvalidSessionException |
NotAuthenticatedException |
InvalidUsernameOrPasswordException |
NotAuthenticatedException |
LockedOutException |
NotAuthenticatedException |
LockoutRegionMismatchException |
NotAuthenticatedException |
OneTimePasswordIncorrectException |
NotAuthenticatedException |
TwoFactorAuthenticationErrorException |
NotAuthenticatedException |
TwoFactorAuthenticationSetupExpiredException |
NotAuthenticatedException |
ApiKeyIsDisabledException |
NotAuthorizedException |
ApiKeyIsPathRestrictedException |
NotAuthorizedException |
ApiKeyOnlyForDesktopAppException |
NotAuthorizedException |
ApiKeyOnlyForMobileAppException |
NotAuthorizedException |
ApiKeyOnlyForOfficeIntegrationException |
NotAuthorizedException |
BillingOrSiteAdminPermissionRequiredException |
NotAuthorizedException |
BillingPermissionRequiredException |
NotAuthorizedException |
BundleMaximumUsesReachedException |
NotAuthorizedException |
CannotLoginWhileUsingKeyException |
NotAuthorizedException |
CantActForOtherUserException |
NotAuthorizedException |
ContactAdminForPasswordChangeHelpException |
NotAuthorizedException |
FilesAgentFailedAuthorizationException |
NotAuthorizedException |
FolderAdminOrBillingPermissionRequiredException |
NotAuthorizedException |
FolderAdminPermissionRequiredException |
NotAuthorizedException |
FullPermissionRequiredException |
NotAuthorizedException |
HistoryPermissionRequiredException |
NotAuthorizedException |
InsufficientPermissionForParamsException |
NotAuthorizedException |
InsufficientPermissionForSiteException |
NotAuthorizedException |
MustAuthenticateWithApiKeyException |
NotAuthorizedException |
NeedAdminPermissionForInboxException |
NotAuthorizedException |
NonAdminsMustQueryByFolderOrPathException |
NotAuthorizedException |
NotAllowedToCreateBundleException |
NotAuthorizedException |
PasswordChangeNotRequiredException |
NotAuthorizedException |
PasswordChangeRequiredException |
NotAuthorizedException |
ReadOnlySessionException |
NotAuthorizedException |
ReadPermissionRequiredException |
NotAuthorizedException |
ReauthenticationFailedException |
NotAuthorizedException |
ReauthenticationFailedFinalException |
NotAuthorizedException |
ReauthenticationNeededActionException |
NotAuthorizedException |
RecaptchaFailedException |
NotAuthorizedException |
SelfManagedRequiredException |
NotAuthorizedException |
SiteAdminRequiredException |
NotAuthorizedException |
SiteFilesAreImmutableException |
NotAuthorizedException |
TwoFactorAuthenticationRequiredException |
NotAuthorizedException |
UserIdWithoutSiteAdminException |
NotAuthorizedException |
WriteAndBundlePermissionRequiredException |
NotAuthorizedException |
WritePermissionRequiredException |
NotAuthorizedException |
ZipDownloadIpMismatchException |
NotAuthorizedException |
ApiKeyNotFoundException |
NotFoundException |
BundlePathNotFoundException |
NotFoundException |
BundleRegistrationNotFoundException |
NotFoundException |
CodeNotFoundException |
NotFoundException |
FileNotFoundException |
NotFoundException |
FileUploadNotFoundException |
NotFoundException |
FolderNotFoundException |
NotFoundException |
GroupNotFoundException |
NotFoundException |
InboxNotFoundException |
NotFoundException |
NestedNotFoundException |
NotFoundException |
PlanNotFoundException |
NotFoundException |
SiteNotFoundException |
NotFoundException |
UserNotFoundException |
NotFoundException |
AlreadyCompletedException |
ProcessingFailureException |
AutomationCannotBeRunManuallyException |
ProcessingFailureException |
BehaviorNotAllowedOnRemoteServerException |
ProcessingFailureException |
BundleOnlyAllowsPreviewsException |
ProcessingFailureException |
BundleOperationRequiresSubfolderException |
ProcessingFailureException |
CouldNotCreateParentException |
ProcessingFailureException |
DestinationExistsException |
ProcessingFailureException |
DestinationFolderLimitedException |
ProcessingFailureException |
DestinationParentConflictException |
ProcessingFailureException |
DestinationParentDoesNotExistException |
ProcessingFailureException |
ExpiredPrivateKeyException |
ProcessingFailureException |
ExpiredPublicKeyException |
ProcessingFailureException |
ExportFailureException |
ProcessingFailureException |
ExportNotReadyException |
ProcessingFailureException |
FailedToChangePasswordException |
ProcessingFailureException |
FileLockedException |
ProcessingFailureException |
FileNotUploadedException |
ProcessingFailureException |
FilePendingProcessingException |
ProcessingFailureException |
FileProcessingErrorException |
ProcessingFailureException |
FileTooBigToDecryptException |
ProcessingFailureException |
FileTooBigToEncryptException |
ProcessingFailureException |
FileUploadedToWrongRegionException |
ProcessingFailureException |
FilenameTooLongException |
ProcessingFailureException |
FolderLockedException |
ProcessingFailureException |
FolderNotEmptyException |
ProcessingFailureException |
HistoryUnavailableException |
ProcessingFailureException |
InvalidBundleCodeException |
ProcessingFailureException |
InvalidFileTypeException |
ProcessingFailureException |
InvalidFilenameException |
ProcessingFailureException |
InvalidPriorityColorException |
ProcessingFailureException |
InvalidRangeException |
ProcessingFailureException |
ModelSaveErrorException |
ProcessingFailureException |
MultipleProcessingErrorsException |
ProcessingFailureException |
PathTooLongException |
ProcessingFailureException |
RecipientAlreadySharedException |
ProcessingFailureException |
RemoteServerErrorException |
ProcessingFailureException |
ResourceLockedException |
ProcessingFailureException |
SubfolderLockedException |
ProcessingFailureException |
TwoFactorAuthenticationCodeAlreadySentException |
ProcessingFailureException |
TwoFactorAuthenticationCountryBlacklistedException |
ProcessingFailureException |
TwoFactorAuthenticationGeneralErrorException |
ProcessingFailureException |
TwoFactorAuthenticationUnsubscribedRecipientException |
ProcessingFailureException |
UpdatesNotAllowedForRemotesException |
ProcessingFailureException |
DuplicateShareRecipientException |
RateLimitedException |
ReauthenticationRateLimitedException |
RateLimitedException |
TooManyConcurrentLoginsException |
RateLimitedException |
TooManyConcurrentRequestsException |
RateLimitedException |
TooManyLoginAttemptsException |
RateLimitedException |
TooManyRequestsException |
RateLimitedException |
TooManySharesException |
RateLimitedException |
AgentUnavailableException |
ServiceUnavailableException |
AutomationsUnavailableException |
ServiceUnavailableException |
MigrationInProgressException |
ServiceUnavailableException |
SiteDisabledException |
ServiceUnavailableException |
UploadsUnavailableException |
ServiceUnavailableException |
AccountAlreadyExistsException |
SiteConfigurationException |
AccountOverdueException |
SiteConfigurationException |
NoAccountForSiteException |
SiteConfigurationException |
SiteWasRemovedException |
SiteConfigurationException |
TrialExpiredException |
SiteConfigurationException |
TrialLockedException |
SiteConfigurationException |
UserRequestsEnabledRequiredException |
SiteConfigurationException |
Folder.listFor("/", null).all()
for (Folder item : Folder.listFor("/", null).listAutoPaging()) {
System.out.println(item.path);
}
ListIterator<Folder> listing = Folder.listFor("/", null);
do {
for (Folder item : listing.loadNextPage()) {
System.out.println(item.path);
}
} while (listing.hasNextPage());
// Will upload a file called "test.txt" and print its size
import com.files.FilesClient;
import com.files.models.File;
import java.io.IOException;
public class App {
public static void main( String[] args ) {
FilesClient.apiKey = "YourAPIKeyHere";
try {
File transferred = File.create("test.txt", null).putLocalFile("test.txt");
System.out.println("TransferedSize:"+transferred.getSize());
} catch (IOException e) {
e.printStackTrace();
}
}
}
File file = File.download("test.txt", null);
try(InputStream inputStream = file.getInputStream()) {
String text = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
.lines()
.collect(Collectors.joining("\n"));
String filecontents = text;
}
File file = File.download("test.txt", null);
file.saveAsLocalFile("/tmp/");
For related documentation see Case Sensitivity Documentation.
if (PathUtils.isSame("Fïłèńämê.Txt", "filename.txt")) {
System.out.println("Paths are the same");
}
Files.com publishes a Files.com API server, which is useful for testing your use of the Files.com SDKs and other direct integrations against the Files.com API in an integration test environment.
It is a Ruby app that operates as a minimal server for the purpose of testing basic network operations and JSON encoding for your SDK or API client. It does not maintain state and it does not deeply inspect your submissions for correctness.
Eventually we will add more features intended for integration testing, such as the ability to intentionally provoke errors.
Download the server as a Docker image via Docker Hub.
The Source Code is also available on GitHub.
A README is available on the GitHub link.