很奇怪的问题,我在一个类文件里的某个方法里调用dll的方法乱码,但是我在同样的类文件里直接在main方法里调用dll的方法就显示正常。这个dll是控制显示器的。
如:public class TestServiceImpl{
    public boolean service(){
        // xxx 得到下面方法AddTextArea的参数,其中text是中文的,①调用了instance.AddTextArea方法后,显示器就会显示这个text的文字,此处调用会显示乱码
        int reslut = instance.AddTextArea(jno, qno, left,top, width, height, fontColor, fontName,fontSize, fontBold, fontItalic, fontUnder,line, hAlign, vAlign, text, type, speed, delay);
        // 省略
    }
    public static void main(String[] args){
        // ②此处调用则显示正常
        int reslut = instance.AddTextArea(jno, qno, left,top, width, height, fontColor, fontName,fontSize, fontBold, fontItalic, fontUnder,line, hAlign, vAlign, text, type, speed, delay);
    }
}
①处是把整个项目在tomcat里跑起来,然后action会调用这个service的方法,显示乱码。
②处是直接在这个类的main方法里运行,中文显示正常。

解决方案 »

  1.   

    用的是JNApublic interface JoymindCommDLLLib extends Library {
            String dllPath = Thread.currentThread().getContextClassLoader().getResource("com/cemso/jna").toString() + "JoymindComm.dll";
            JoymindCommDLLLib INSTANCE = (JoymindCommDLLLib) Native.loadLibrary((Platform.isWindows() ? dllPath : "c"), JoymindCommDLLLib.class);        public int addTextArea(int jno, int qno, int left, int top, int width, int height, int fontColor, String fontName, int fontSize, int fontBold, int fontItalic, int fontUnder, int line,
                    int hAlign, int vAlign, String text, int type, int speed, int delay);      
        }
      

  2.   

    是否是tomcat 传参时charset没有设置为utf-8 ?
      

  3.   

    先不说楼主的问题,楼主的addTextArea方法很蛋疼,竟然有十几个参数,维护起来极其困难,如果是我的话肯定会把这十几个参数封装成一个类叫做TextAreaParam,这种重构方法叫做"引入参数对象",参数对象可以是简单的javaBean,也可以做成final的,甚至可以引入builder模式.至于楼主的问题,在tomcat中跑可能编译格式已经发生了变化,你试试将text字符串转换一下:
    String newText = new String(text.getBytes("ISO-8859-1"), "GBK");
      

  4.   

    楼上两位,楼主的代码调用根本不涉及Tomcat传参。纯粹是掉对象里面的方法。to 飞翔猫咪号,dll里面定义的方法,你用JNA是无法改变此方法的参数类型的。感谢两位。
      

  5.   


    我在同一个类文件里调同样的一段代码,只不过一个是在main方法里调用。有一个现实正常,说明编码没问题。
    难道是编译成.class编码变了?
      

  6.   


    我在传入这个dll方法的时候都是正常的,所有编码都GBK。
      

  7.   

    建议把项目导出成war包,再部署,我有遇到过同样的问题。
      

  8.   

    我也遇到这个问题,花了好大劲才解决
    如果是WEB工程,不会有问题
    如果是客户端,要求你的入口程序(Main方法)必须是GBK的
    发送数据的那个类也必须是GBK的,中间经过调用的类就无所谓了