Skip to content

Instantly share code, notes, and snippets.

@boycaught
Created July 17, 2012 06:24
Show Gist options
  • Save boycaught/3127580 to your computer and use it in GitHub Desktop.
Save boycaught/3127580 to your computer and use it in GitHub Desktop.
CF+Java file downloader
<!--- suggested on: http://stackoverflow.com/questions/4738610/downloading-large-file-in-coldfusion-using-cfhttp --->
<cfscript>
source = 'protocol://domain/path/to/file.ext';
destination = getDirectoryFromPath(getCurrentTemplatePath()) & listlast(source, "/");
// bytearray routine
private binary function getByteArray (
required numeric size
)
output = false
{
var emptyByteArray = createObject("java", "java.io.ByteArrayOutputStream").init().toByteArray();
var byteClass = emptyByteArray.getClass().getComponentType();
var byteArray = createObject("java","java.lang.reflect.Array").newInstance(byteClass, arguments.size);
return byteArray;
}
uri = createObject("java", "java.net.URL").init(source);
uis = uri.openStream();
bis = createObject("java", "java.io.BufferedInputStream").init(uis);
fos = createObject("java", "java.io.FileOutputStream").init(destination);
bos = createObject("java", "java.io.BufferedOutputStream").init(fos);
buffer = getByteArray(1024);
len = bis.read(buffer);
while(len > 0) {
bos.write(buffer,0,len);
len = bis.read(buffer);
}
bos.close();
bis.close();
fos.close();
uis.close();
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment