ColdFusion Socket Streaming,目标上未正确接收UTF-8字符串

时间:2013-06-03 07:25:15

标签: coldfusion coldfusion-8

我使用easySocket udf和下面的代码通过tcp / ip发送实际消息的2字节标头长度。但是,UTF-8字符(即()(8D十六进制)被接收为?(3F hex))。我使用了以下代码,我也检查了stackoverflow。有没有办法发送相当于长度的字节,以便不涉及字符编码?感谢。

    <cffunction name="easySocket" access="private" returntype="any" hint="Uses Java Sockets to connect to a remote socket over TCP/IP" output="false">

        <cfargument name="host" type="string" required="yes" default="localhost" hint="Host to connect to and send the message">
        <cfargument name="port" type="numeric" required="Yes" default="8080" hint="Port to connect to and send the message">
        <cfargument name="message" type="string" required="yes" default="" hint="The message to transmit">

        <cfset var result = "">
        <cfset var socket = createObject( "java", "java.net.Socket" )>
        <cfset var streamOut = "">
        <cfset var output = "">
        <cfset var input = "">

        <cftry>
            <cfset socket.init(arguments.host,arguments.port)>
            <cfcatch type="Object">
                <cfset result = "#cfcatch.Message#<br>Could not connected to host <strong>#arguments.host#</strong>, port <strong>#arguments.port#</strong>.">
                <cfreturn result>
            </cfcatch>
        </cftry>

        <cfif socket.isConnected()>
            <cfset streamOut = socket.getOutputStream()>

            <cfset output = createObject("java", "java.io.PrintWriter").init(streamOut)>
            <cfset streamInput = socket.getInputStream()>

            <cfset inputStreamReader= createObject( "java", "java.io.InputStreamReader").init(streamInput)>
            <cfset input = createObject( "java", "java.io.BufferedReader").init(InputStreamReader)>


            <cfset output.print(chr(1) & chr(141) & arguments.message)>
            <cfset output.flush()>

            <cfset result='Success: '& arguments.message >
            <cfset socket.close()>
        <cfelse>
            <cfset result = "Could not connected to host <strong>#arguments.host#</strong>, port <strong>#arguments.port#</strong>.">
        </cfif>

        <cfreturn result>
    </cffunction>

    <cfset txt_msg ='<COS MSG_TYPE_ID="0100" MCC="5999" POS_ENTRY_MODE="012" POS_COND="00" CARD_NUMBER="6417801234567890" ACQUIRER_ID="00000002" TERMINAL_ID="CAL100  " PROC_CODE="300000" STAN="000129" RETRIEVAL_REFERENCE="330145181129" DATE_LOCAL_TRAN="1111" TIME_LOCAL_TRAN="173654" TRANSM_DATE_TIME="1111173654" CARD_ACPT_ID_CODE="TESTDTA" CARD_ACPT_NAME_LOC="TSTESTSTE" />'>

    <cfoutput>#easySocket('172.16.0.23','9095',txt_msg)#</cfoutput>

1 个答案:

答案 0 :(得分:1)

According to the API,“...如果省略编码标识符,InputStreamReaderOutputStreamWriter依赖于默认编码。” - 可能不是“UTF-8”。在Windows机器上通常是Cp1252。您可以通过检查系统属性来查看默认值:

    <cfset system = createObject("java", "java.lang.System")>
    <cfset defaultEncoding = system.getProperty("file.encoding", "unknown")>

..或PrintWriter的编码:

    <cfdump var="#output.getEncoding()#">

无论如何,尝试在创建输出流时明确指定编码。这应该可以解决问题。

 ... 
 <cfset streamOut = socket.getOutputStream()>
 <cfset writer = createObject("java", "java.io.OutputStreamWriter").init(streamOut, "UTF-8")>
 <cfset output = createObject("java", "java.io.PrintWriter").init(writer)>
 ...