[ptt-users] More on getting non-UTF-8 content

Mark.Lutton at thomson.com Mark.Lutton at thomson.com
Wed Dec 6 13:57:07 PST 2006


Followup on previous message:  Here is the code that does not work.
 
AppletsJar = self.get( '''http://localhost:8080/MyApp/applets/Applets.jar''' )
responseContent = self.response.getContent()
jarFile = open("Applets.jar", 'wb')
jarFile.write(responseContent)
jarFile.close()
print "Applets.jar written"
 
As noted, x'85' in the file is translated to x'26', corrupting the Jar file.
 
Fortunately you can write Java code (from a working Java program) in Jython.  The following code in the testcase works correctly:
 
from java.net import URL, URLConnection
from java.io import DataOutputStream, FileOutputStream, DataInputStream
 
myURL = URL('''http://localhost:8080/MyApp/applets/Applets.jar")
cc = myURL.openConnection()
jarFile = DataOutputStream(FileOutputStream("Applets.jar")
inStream = DataInputStream(cc.getInputStream())
inNum = inStream.read()
while -1 != inNum:
    jarFile.write(inNum)
    inNum = inStream.read()
jarFile.close()
print "Applets.jar written"
 
 
So I can work around the problem by writing Java code in a Python script.  (The saved file is identical to the file on the server.)
 
Is there any way I can make the first version work?
 
 
Mark Lutton
Business Intelligence Services, a Thomson Business
 


More information about the Users mailing list