在 google 搜ruby execute jar encoding,
参考了Need to specify -Dfile.encoding when executing jar created by Warbler #368试试,发现的确是编码问题,java 执行时要用-Dfile.encoding=UTF-8
显示指定编码
sign = IO.popen(["java", "-Dfile.encoding=UTF-8", "-jar", "#{Rails.root}/lib/RSA_UTF.jar", key, message]).read.gsub("\n", "")
然后中文就能正常识别了,之前丢进去 java 的 main 函数会变成问号
有个地方需要用合作方提供的签名程序生成签名。由于程序是个 jar 包,我用IO.popen
调用
# key 固定
# message 参数Hash to_json
sign = IO.popen(["java", "-jar", "#{Rails.root}/lib/sign.jar", key, message]).read.gsub("\n", "")
并把参数和签名输出到日志
D, [2017-01-11T17:04:52.852529 #3898] DEBUG -- : message:
D, [2017-01-11T17:04:52.852572 #3898] DEBUG -- : {"touristList":[{"name":"fu/ting","firstname":"fu","lastname":"ting","psptType":"2","psptId":"quirky","birthday":"1990-12-22","tel":"","touristType":0,"psptEndDate":"2025-12-22","sex":1,"hkvisaType":0,"macvisaType":0,"hkmacIssueOrgan":"","issueOrgan":"0","country":"中国"}]}
D, [2017-01-11T16:57:37.236836 #3898] DEBUG -- : sign:
D, [2017-01-11T16:57:37.236836 #3898] DEBUG -- : lQHTGqE0nLjy3Z6OTlIULT3pvdvizWgoLg2qZFM1BIJ80ERlrdudKCvm8Rgbs9LqsDs3SPfjRXWOJvEJUW0PMg==
发现只要参数里面包含中文的话,生成的签名就不对。
但是,当我把日志的参数原封不动复制到 rails c 执行时,生成的签名却是对的。。
message:
{"touristList":[{"name":"fu/ting","firstname":"fu","lastname":"ting","psptType":"2","psptId":"quirky","birthday":"1990-12-22","tel":"","touristType":0,"psptEndDate":"2025-12-22","sex":1,"hkvisaType":0,"macvisaType":0,"hkmacIssueOrgan":"","issueOrgan":"0","country":"中国"}]}
sign:
Yi7thHvFlJZ45s4bXPMqK2JPN0BELF7HyJrWs6wETrkmGJI24DcEctO8/ribSAGSr6Z/AdA0qXOu7K/nNFBR+Q==
想不通