You can do this by calling Java from CF code. Buffered input and output clusters of a stream are designed to be stored on pieces of data, and not everything, which helps to avoid OutOfMemory errors.
getByteArray() is a helper method because there is no way to declare something like byte buf[]=new byte[1024]; in CF directly.
In the example, change the source and destination variables.
Example
<cfset source = 'protocol://domain/path/to/file.ext'> <cfset destination = getDirectoryFromPath(getCurrentTemplatePath()) & listlast(source, "/")> <cffunction name="getByteArray" access="private" returnType="binary" output="no"> <cfargument name="size" type="numeric" required="true"/> <cfset var emptyByteArray = createObject("java", "java.io.ByteArrayOutputStream").init().toByteArray()/> <cfset var byteClass = emptyByteArray.getClass().getComponentType()/> <cfset var byteArray = createObject("java","java.lang.reflect.Array").newInstance(byteClass, arguments.size)/> <cfreturn byteArray/> </cffunction> <cfscript> 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>
orangepips
source share