Backendless Support

Downloading files from Backendless file hosting

Once a developer has uploaded files to the Backendless Hosting system, the file gets a public URL which can either be obtained using Backendless console or calculated using the following template:

<a href="<application"><application</a> id>/<version name="">/files/<path>/<file name=""></file></path></version>
Alternatively, when a file is uploaded, the API call returns the URL of the uploaded file. The sample code below demonstrates how to download the file.

The code prints out the contents of the file to system console, but it can be easily modified to store it in the local file system, or transfer elsewhere over the network, etc:

public static void downloadFile( BackendlessFile backendlessFile ) throws IOException
 URL url = new URL( backendlessFile.getFileURL() );
 HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
 int responseCode = httpConn.getResponseCode();
 // always check HTTP response code first
 if( responseCode == HttpURLConnection.HTTP_OK )
 // opens input stream from the HTTP connection
 InputStream inputStream = httpConn.getInputStream();
 // opens an output stream to save into file
 OutputStream outputStream = System.out;
 int bytesRead = -1;
 byte[] buffer = new byte[ 4096 ];
 System.out.println( "File content is:\n===========================" );
 while( (bytesRead = buffer )) != -1 )
 outputStream.write( buffer, 0, bytesRead );
 System.out.println( "===========================" );
 System.out.println( "File downloaded" );
 System.out.println( "No file to download. Server replied HTTP code: " + responseCode );

 Review related topics:

Is article helpful?