I am creating a gateway script that will download a text file from a corporate server. The text file is small only a few KBs. I'm not a Python/Java programmer so this is a bit foreign to me.
I started with the built-in FTPlib which would allow me to transfer the file but while the script would complete almost immediately, it would be several minutes before data was in the text file.
Searching the forums ftp-client-speed-via-scripting and online pointed me in the direction of using the java.net.urlconnection method. I found several examples of this and have it downloading the file in a few seconds.
Except, the file is "binary gibberish" not human readable text. I believe this has to do with InputStream but not sure where/how I need to convert it to human readable text.
import os
from java.net import URL
from java.net import URLConnection
from java.io import BufferedReader
from java.io import InputStream
from java.io import InputStreamReader
downloadFile("dwb.dat", "C:\\FILES\\")
def downloadFile(fileName, folder):
inputStream = getInputStream("user", "password", "127.0.0.1", "usr/dwb.dwn")
streamReader = InputStreamReader(inputStream)
reader = BufferedReader(streamReader)
fileData = []
line = reader.readLine()
while line != None:
fileData.append(line)
line = reader.readLine()
inputStream.close()
saveFilePath = os.path.join(folder, fileName)
try:
file = open(saveFilePath, 'w')
print saveFilePath
for line in fileData:
file.write("%s\n" % (line))
print "%s\n" % (line)
file.close()
print "File Written"
except Exception, ex:
writeMessage("Error writing %s : %s" % (fileName, str(ex)))
def getInputStream(user, pwd, IP, filename):
ftpUrl = "ftp://%s:%s@%s/%s" % (user, pwd, IP, filename)
url = URL(ftpUrl)
try:
url.close()
except:
pass
FTPConnection = url.openConnection()
inputStream = FTPConnection.getInputStream()
url.close()
return inputStream
The trick is to go "all in" on Java standard library stuff and avoid Jython's entirely, basically. That is, return a Java byte array instead of a Jython list, etc. The transferTo function is an interface default method that's "new" (as of Java 9) so lots of online resources aren't aware of it yet, but it automatically does a buffered transfer from an input stream to an output stream. I wrote the method to return a plain byte array because that gives you maximum flexibility; the builtin system.file.writeFile method can handle that byte array directly and write it out to an actual file.
Be very careful with ChatGPT w.r.t. Ignition. It's not even close to good enough to be blindly trusted. In the very first answer I got, it gave me Java code and imported a third party library when I explicitly asked for Jython and the Java standard library. In the second answer, when coached, it still gave me an unnecessarily complicated answer that wasn't aware of the transferTo function. If I didn't already know what I was doing, I wouldn't have been able to 'coach' it through these things or fix the final snippet.