Skip to content

Instantly share code, notes, and snippets.

@benjsicam
Last active December 27, 2023 23:59
Show Gist options
  • Save benjsicam/5563009 to your computer and use it in GitHub Desktop.
Save benjsicam/5563009 to your computer and use it in GitHub Desktop.
A code snippet that demonstrates how to download a file from the NetSuite file cabinet and save onto a local folder.
private void getFileAndSaveLocally() throws IOException {
//Login to NetSuite Web Service
/*
* Get the File from NetSuite by invoking the get operation
*/
_console.writeLn("Enter Internal ID of the File: ");
String internalID = _console.readLn();
RecordRef NSFileRef = new RecordRef();
NSFileRef.setInternalId(internalID);
NSFileRef.setType(RecordType.file);
BaseRef baseRef = (BaseRef) NSFileRef;
ReadResponse response = _port.get(baseRef);
if(response.getStatus().isIsSuccess()) {
File NSFile = (File) response.getRecord();
byte[] fileContents = NSFile.getContent();
/*
* By Default, Netsuite Saves the file in the file cabinet
* with the file extension when you upload it. It is also unique
* because the File Name field is a keyed field. For sampling purposes,
* we will assume that their are no conflicts with other file names
* in the local folder.
*/
String fileName = NSFile.getName();
/*
* Determine the folder path
*/
Path folderPath = Paths.get("C:\\Downloads\\");
/*
* Determine if the folder exists in the file system
*/
if(!Files.exists(folderPath))
Files.createDirectory(folderPath);
/*
* Determine the file path
*/
Path filePath = Paths.get("C:\\Downloads\\", fileName);
/*
* Determine the folder path
*/
Files.createFile(filePath);
/*
* Write the contents onto the file
*/
try(FileOutputStream fileOutputStream = new FileOutputStream(filePath.toFile())) {
fileOutputStream.write(fileContents);
}
}
else {
_console.writeLn("Failed");
}
//Log out of NetSuite Web Service
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment