ABN 25 173 915 011 markomedia - web development

markomedia - web development

  • Home
  • Contact
  • Blog

ColdFusion

  • markomedia
    • Tag ColdFusion
Share |
  • September 10, 2011

    Unzip files with ColdFusion MX 7 using native Java classes

    Author
    Marko Tomic

    This week I had to unzip some files using ColdFusion 7 (I know it’s ancient technology) and I realised that CF7 doesn’t natively support zipping/unzipping of files!  Fortunately, ColdFusion can utilise the underlying power of Java to achieve this.  I found this neat Java article – Unzipping Files with java.util.zip.ZipFile and simply translated it into ColdFusion 7.

    Here is the ColdFusion version that works well on CFMX7:

    <cfparam name="dest" default="/path_to_extract/">
     
    <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>
    	if(structKeyExists(form,"myFile") and len(form.myFile)) {
    		content = ArrayNew(1);
    		FileOutputStream = createObject('java',"java.io.FileOutputStream");
    		BufferedOutputStream = createObject('java',"java.io.BufferedOutputStream");
    		ZipFile = createObject('java',"java.util.zip.ZipFile");
    		ioFile = createObject('java',"java.io.File");
    		Byte = createObject('java',"java.lang.Byte");
     
    		buffer = getByteArray(1024);
    		length = 0;
     
    		zipFileName = form.myFile;
     
    		zFile = ZipFile.init(zipFileName);
    		entries = zFile.entries();
     
    		while(entries.hasMoreElements()) {
    			entry = entries.nextElement();
     
    			if(entry.isDirectory()) {
    				// Assume directories are stored parents first then children.
    				ioFile.init(dest & entry.getName()).mkdir();
    				continue;
    			}
    			in = zipFile.getInputStream(entry);
    			out = BufferedOutputStream.init(FileOutputStream.init(dest & entry.getName()));
     
    			length = in.read(buffer);
    			while(length gte 0) {
    				 out.write(buffer, 0, length);
    				 length = in.read(buffer);
    			}
     
    			in.close();
    			out.close();
    		}
     
    		zFile.close();
     
    		//loop tghrough content array and decrypt
    	}
    </cfscript>
     
    <form name="myForm" method="POST" action="zip.cfm" enctype="multipart/form-data">
    	<input type="file" name="myfile" />
    	<input type="submit" name="submit" value="Upload" />
    </form>

    Cheers
    Marko

    Top
  • September 30, 2010

    Monitor Railo on Tomcat using JConsole

    Author
    Marko Tomic

    At Learnosity, I’ve been trying to get to the bottom of a “Java Heap Space” error on my local Railo installation and decided to use JConsole to monitor Tomcat server.  JConsole is a very useful Java application monitoring tool, which is bundled into every JRE.  You can run it by simply executing jconsole binary:

    $JAVA_HOME/bin/jconsole

    Where $JAVA_HOME is the path to your Java installation.

    By default, JConsole picks up all local Java processes, which you can monitor individually.  Unfortunately, Tomcat doesn’t seem to be listed under local processes and I had to use remote connection instead.  The way to connect to Tomcat with JConsole is a little tricky and this is what you need to do:

    You need to tell Tomcat to accept clients on TCP port 8999, on the host “localhost”, without authentication.

    DO NOT disable remote authentication in production environment. It should only be done for testing purposes in development/staging environments. close

    To translate this into code, set CATALINA_HOME environment variable to this:

    $CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=localhost";
    $ export CATALINA_OPTS;

    (Re)start Tomcat and you should now be able to monitor it on localhost:8999
    Here are some JConsole screen shots:
    JConsole 1 JConsole 2 JConsole 3JConsole 4 JConsole 5 JConsole 6
    References:
    Monitoring Tomcat with JMX
    Using JConsole to Monitor Applications

    Top

    Navigation

    • Home
    • Contact
    • Blog

    Archives

    • April 2012
    • March 2012
    • February 2012
    • January 2012
    • December 2011
    • November 2011
    • October 2011
    • September 2011
    • August 2011
    • July 2011
    • May 2011
    • December 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009

    From the blog

    • Compile PHP pcntl module on OS X Lion

    • MySQL cursors in stored procedures

    • IE6, IE7, IE8, & IE9 on OS X in Virtual Machine

    • opendiff and FileMerge on OS X

    • Bandwidth throttling on OS X

    About us

    Marko Tomic - Web professional and an Adobe Certified Expert with over 10 years of commercial experience using variety of technologies.

    Connect

    Facebook icon Twitter icon Email icon RSS icon