1

我正在使用easySocket udf和下面的代码通过 tcp/ip 发送实际消息的 2 字节标头长度。然而,UTF-8 字符(即 ( )(8D 十六进制)被接收为?(3F 十六进制))。我使用了以下代码,我也在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>
4

1 回答 1

1

根据 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)>
 ... 
于 2013-06-03T17:41:58.353 回答