Last active
April 25, 2017 13:01
-
-
Save cflove/8150328 to your computer and use it in GitHub Desktop.
Get large file over HTTP. CFHTTP in some cases seems not to respect timeout attribute values and time out prematurely. CFHTTP collect the file into the memory and write to the hard drive at the end. That enables large files to create an OutOfMemoryError error. This should solve both of those options.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name="httpget" access="private" returnType="any" output="No"> | |
<cfargument name ="source" type="string" required="true"/> | |
<cfargument name ="destination" type="string" required="true"/> | |
<cfargument name ="ConnectTimeout" type="numeric" required="false" default="10" hint="Time Out in Seconds"/> | |
<cfargument name ="ReadTimeout" type="numeric" required="false" default="60" hint="Time Out in Seconds" /> | |
<cfargument name ="dimensions" type="numeric" required="false" default="255"/> | |
<cfset local.urlconnection = createObject("java", "java.net.URL").init(arguments.source)> | |
<cfset local.connection = local.urlconnection.openConnection() /> | |
<cfset local.connection.setConnectTimeout(javaCast("int",arguments.ConnectTimeout*1000)) /> | |
<cfset local.connection.setReadTimeout(javaCast("int",arguments.ReadTimeout*1000)) /> | |
<cfset local.InputStream = local.connection.getInputStream()> | |
<cfset local.BufferedInputStream = createObject("java", "java.io.BufferedInputStream").init(local.InputStream)> | |
<cfset local.FileOutputStream = createObject("java", "java.io.FileOutputStream").init(destination)> | |
<cfset local.BufferedOutputStream = createObject("java", "java.io.BufferedOutputStream").init(local.FileOutputStream)> | |
<cfset local.ByteArrayOutputStream = createObject("java", "java.io.ByteArrayOutputStream").init().toByteArray()/> | |
<cfset local.thisClass = local.ByteArrayOutputStream.getClass().getComponentType()/> | |
<cfset local.buffer = createObject("java","java.lang.reflect.Array").newInstance(local.thisClass, javaCast("int",arguments.dimensions) )/> | |
<cfset local.length = local.BufferedInputStream.read(local.buffer) /> | |
<cfloop condition="local.length gt 0"> | |
<cfset local.BufferedOutputStream.write(local.buffer,0,local.length) /> | |
<cfset local.length = local.BufferedInputStream.read(local.buffer) /> | |
</cfloop> | |
<cfset local.BufferedOutputStream.close() /> | |
<cfset local.BufferedInputStream.close() /> | |
<cfset local.FileOutputStream.close() /> | |
<cfset local.InputStream.close() /> | |
</cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment