Railo


Railo Server is a fast, free, easy-to-use open-source web application server. It will get you up and running in minutes, and with it you can deploy powerful CFML web applications across many platforms.

JAVA and Railo work together well. You can call JAVA classes right from your coldfusion code on Railo.

In this example, I was looking for an event to be fired when I was using CFHTTP to download a file on the server side. Unfortunately, there was no associated event with CFHTTP. So I decided to utilize JAVA's urlconnection class.

<cffunction name="httpget" access="remote" output="no" returntype="string" returnformat="plain">
    <cfargument name ="source"	type="string" required="true"/>
    <cfargument name ="vFile" 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"/>
    
    <cfif FileExists("C:\myPath\#arguments.vFile#")>
        <cfreturn "Already Exists">
    <cfelse>
        <cfset local.urlconnection = createObject("java", "java.net.URL").init(arguments.source)>
        <cfset local.connection = local.urlconnection.openConnection() />
        <cfset local.contentLength = local.connection.getContentLength() />
        <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("C:\myPath\" & vFile)>
        <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) />
        <cfset offset = 0>
        
        <cfloop condition="local.length gt 0">
            <cfset local.BufferedOutputStream.write(local.buffer,0,local.length) />
            <cfset local.length = local.BufferedInputStream.read(local.buffer) />
            <cfset offset = offset + local.length>
            <cfset pTotal = (offset / local.contentLength) * 100>
            <cffile file="C:\myPath\downloadLog.txt" action="write" nameconflict="overwrite" output="#int(pTotal)#">
        </cfloop>
            <cffile file="C:\myPath\downloadLog.txt" action="write" nameconflict="overwrite"  output="100">
        <cfset local.BufferedOutputStream.close() />
        <cfset local.BufferedInputStream.close() />
        <cfset local.FileOutputStream.close() />
        <cfset local.InputStream.close() />
        
        <cfreturn local.contentLength />
    </cfif>
</cffunction>