coldfusion core java instead of cffile

588 views Asked by At

i am using the cffile tag in a coldfusion 2016 app(website) to upload images from the user to the server.

i have been ordered to stop using cffile and start using the underlying java instead because it is more efficient.

i was given this code which shows how to take a dir of files and zip them with the core java. But was not smart enough to use it for my needs.

i need a page to receive a simple POST and move the file posted to a specific directory on the server. usually on the "action" page of a website i would use:

cffile action="upload" filefield="FieldNameFromPostedForm" destination="MyDirectoryOnServer"  

can anyone point me in a direction to see under the hood of coldfusion so i can end up with some magic like:

FileOutputStream = createobject("java", "java.io.FileOutputStream"); 

in a function.

thanks!

Progress Report:
I am closer to what i was told to do. the file is written to the server but i am getting an error afterwards so i am missing a piece of the puzzle

<cfscript>
function bj_script(source, server_dest_Path) {
try { 
    FileOutputStream = createobject("java", "java.io.FileOutputStream"); 
    BufferedOutputStream = createobject("java", "java.io.BufferedOutputStream"); 
    FileInputStream = createobject("java", "java.io.FileInputStream"); 
    BufferedInputStream = createobject("java","java.io.BufferedInputStream");

    the_file = createobject("java","java.io.File"); 
    the_file.init(source);

    FileInputStream.init(the_file);
    BufferedInputStream.init(FileInputStream); 
    FileOutputStream.init(server_dest_Path); 
    BufferedOutputStream.init(FileOutputStream); 

    thisbyte = 0; 
    while (thisbyte neq -1){ 
        thisbyte = BufferedInputStream.read(); 
        if (thisbyte neq -1){ 
            BufferedOutputStream.write(thisbyte); 
        } 
    } 

    FileOutputStream.close(); 
    BufferedOutputStream.close(); 
    return true; 
} 
catch(Any exception) { 
    return exception; 
} 
}
</cfscript>

<form method="post" enctype="multipart/form-data">
<input type="File" name="test"><br>
<input type="Submit"><br>
</form>

<cfif request_method is "post">
<cfset ccc = bj_script(form.test, "C:\inetpub\wwwroot\mywebsite\temp_img\blah.jpg") />
</cfif>

after the image in my test uploads i get an error returned of type " java.io.IOException" with the message being "Stream Closed"
i must be doing the while loop incorrectly. can any java nerds tell me what to change?

1

There are 1 answers

1
Daryl Banttari On

Here's something I wrote a few years ago to do nearly that exact thing with a ColdFusion ResultSet. Should serve as a decent primer on how to connect to a Java class from CF and write files.

https://cfprimer.blogspot.com/2009/01/coldfusion-fast-query-to-file-export.html

i have been ordered to stop using cffile and start using the underlying java instead because it is more efficient.

It may be worth pointing out that CFFile also uses the underlying Java to write the file?