【问题标题】:JbossTextMessage Unicode convert failed in LinuxJbossTextMessage Unicode 转换在 Linux 中失败
【发布时间】:2011-03-29 09:54:27
【问题描述】:

我正在尝试上传一个 xml (UTF-8) 文件并将其发布到 Jboss MQ 上。从侦听器读取文件时,UTF-8 字符仅在 Linux 上运行的 Jboss (jboss-5.1.0.GA-3) 实例中格式不正确。

例如:BORÅS 在 Linux jboss 实例中转换为 BOR¿S

当我复制并配置相同的 jboss 实例以在 Windows (SP3) 上运行时,它可以完美运行。

我还通过在 .bashrc 和 run.sh 文件中包含 JAVA_OPTS=-Dfile.encoding=UTF-8 来更改 Linux 中的默认设置。

在侦听器中 JbossTextMessage.getText() 带有错误指定的字符。

有什么建议或解决方法吗?

【问题讨论】:

  • 请提供有关如何上传文件的确切信息。
  • 我已经暴露了一个jsp来使用post方法上传一个xml文件。我将使用 Action 类中的 struts 将文件作为 FormFile 获取,如下所示: FormFile file = theForm.getFile();其次,我将文件数据检索为字节数组 byte[] buf = file.getFileData();最后,我在 Jboss (jboss-5.1.0.GA-3) 的队列中发布。
  • 流程如下:(1)上传XML ---> (2)从文件中获取byte[] ---> (3)放到JMS队列中---> (4) 由 MDB 选择进行处理。当从侦听器收到发布的消息时,我尝试将其转换如下但没有用: ((TextMessage) message).getText().getBytes("UTF-8")

标签: unicode jboss jms message-driven-bean mq


【解决方案1】:

最后我找到了解决方案,但解决方案仍然是一个黑匣子。如果有人知道为什么它失败/成功,请更新线程。

解决方案一目了然: 1. 将文件内容捕获为字节数组,并使用 FileOutputStream 将其写入 jboss tmp 文件夹中的 xml 文件

  1. 当发布到 jboss 消息队列时,我使用了明确编写的 xml 文件(第一步),使用 FileInputStream 作为字节数组并将其作为消息正文传递。

代码示例:

查看:带有 FormFile 的 JSP 页面

控制器类:UploadAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
   ...........

   writeInitFile(theForm.getFile().getFileData()); // Obtain the uploaded file

   Message msg = messageHelper.createMessage( readInitFile() ); // messageHelper is a customized factory method to create Message objects. Passing the newly    
   wrote file's byte array.

   messageHelper.sendMsg(msg); // posting in the queue

   ...........
}

private void writeInitFile(byte[] fileData) throws Exception{

   File someFile = new File("/jboss-5.1.0.GA-3/test/server/default/tmp/UploadTmp.xml");  // Write the uploaded file into a temporary file in jboss/tmp folder
   FileOutputStream fos = new FileOutputStream(someFile);

   fos.write( fileData );

   fos.flush();
   fos.close();     
}

private byte[]  readInitFile() throws Exception{

   StringBuilder buyteArray=new StringBuilder();

   File someFile = new File("/jboss-5.1.0.GA-3/test/server/default/tmp/UploadTmp.xml");  // Read the Newly created file in jboss/tmp folder

   FileInputStream fstream = new FileInputStream(someFile);

   int ch;
   while( (ch = fstream.read()) != -1){
        buyteArray.append((char)ch);
   }
   fstream.close();

   return buyteArray.toString().getBytes();   // return the byte []
}

脚注:我认为这与 Linux/Windows 默认文件保存类型有关。例如:Windows 默认:ANSI。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-14
    • 2018-04-25
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多