Skip to main content
Question

Java code to the download the file with byte data using box

  • May 22, 2025
  • 4 replies
  • 48 views

Forum|alt.badge.img
Hi Team,
We will upload 5GB data into the box and when we download that 5GB it might take time by the developer token will expire or some network issues while downloading.

Could you please help to provide the java code snippet to download the file with byte data and read the outstream data which was downloaded consists of actual file data.

Thanks & Regards,

Kalyan

4 replies

Forum|alt.badge.img

Hey , 

 

If you are using the Java SDK you can take a look here to see all functions you can perform including upload and download

 

As far as downloading a file though, this should work:

BoxFile file = new BoxFile(api, "id");
BoxFile.Info info = file.getInfo();

FileOutputStream stream = new FileOutputStream(info.getName());
file.download(stream);
stream.close();

However a developer token cannot be refreshed. If you wish to refresh your token I would suggest either JWT or Oauth 2.0, instructions for both can be found here

 

Before we dive deeper into those, I am curious as to what your use case is? From there we can determine which type of auth is best suited for your needs.

 

Please let us know, thanks! 


Forum|alt.badge.img

Hi Cary,

To our business requirement, we have huge assets in box to download/upload to another server.

we do have multi-part upload while we do upload assets in box, do we have any multi-part download option which gives faster response when we do it via box-api and programatically.

if we don't have multi-download option available, will it leads us to a connectivity issue during huge asset downloading when we do it via box-api? is there way to get status/flag upon downloading assets/failures/success from box ?

 

Thanks,

Kalyan.


Forum|alt.badge.img

Hey , 

 

If network connectivity is an potential issue I would recommend taking a look at the `downloadRange()` functions and its overloads in the `BoxFile` class seen here

 

This function will allow you to download a file's contents while specifying the range in bytes. If anything were to happen you can then start downloading again from that specified range where you left off. This will allow you to download your large content in discrete chunks. 

 

 


Forum|alt.badge.img

When working with bytes data that I want to convert to a string in Python, how do I determine the correct encoding to use for the conversion? Is there a recommended approach or a method to identify the encoding?