0

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?

3
  • 1
    Your google search string is java upload file to server. Commented Sep 11, 2017 at 21:37
  • 2
    I'm curious in what way java is more efficient? I can think of some areas, but they are specific scenarios. For instance if you need to load a very large file in memory and do string manipulation, ColdFusion will read the entire file. If you're just copying from one folder to another, I assume CF is using the same file streams behind the scenes which writes out bytes as it reads them. Do you have some benchmark to ensure your Java version is actually improving anything? Commented Sep 11, 2017 at 21:49
  • nope no benchmarks other than a crude stopwatch timer on a set of 10 images being uploaded and processed. its the old story of another coder telling my boss that i am doing it wrong and so now they are forcing me to change it. Commented Sep 13, 2017 at 12:18

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

CFFile also uses the underlying Java: yea i tried to tell my bosses that but they were not having it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.