0

我正在尝试上传一个 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 中的默认设置。

Listener JbossTextMessage.getText() 内部带有错误指定的字符。

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

4

1 回答 1

0

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

解决方案一目了然: 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。

于 2010-08-13T14:35:48.203 回答