Download a large file in ColdFusion using CFHTTP

I am trying to upload a large (600 MB) binary to a server using CFHTTP on Coldfusion 8:

<cfhttp method="get" url="#fileURL#" path="#filePath#" file="#fileName#" timeout="600" getasbinary="auto" > 

It works fine for smaller files (100something MB), but for large, I get a Server 500 error: "java.lang.OutOfMemoryError: Java heap space".

The file is downloaded from the Dropbox folder - therefore, only the HTTP GET option is available.

Does anyone know how to download it so that it does not kill the server or timeout?

+6
coldfusion download coldfusion-8
source share
2 answers

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> 
+14
source

The problem is that it is too big. ColdFusion reads the whole thing in memory before writing it to disk.

You would be better off using some other method to get the file. wget can execute HTTP requests from the command line. This with reasonable use of CFEXECUTE is probably a good way.

+2
source

All Articles