Camel FTP中文目录解决办法

来源:互联网 发布:淘宝天猫lee鼎汉店真假 编辑:程序博客网 时间:2024/05/30 23:01
p;    在Camel中使用FTP只需要简单的DSL配置就可以了,把对应的jar放到classpath即可,但是在使用中遇到了FTP服务器上中文目录的问题,如果FTP服务器上的目录是中文的,那么FTP执行什么信息都没有,也没有错误,也没有下载下来。搞得我有些莫名其妙,不知道到底是怎么回事。

 

         后来开始跟踪Camel源代码进行debug,终于发现在org.apache.camel.component.file.remote.FtpOperations类的doChangeDirectory方法中,无法进入到对应的中文目录。

 1   private void doChangeDirectory(String path) { 2         if (path == null || ".".equals(path) || ObjectHelper.isEmpty(path)) { 3             return; 4         } 5  6         log.trace("Changing directory: {}", path); 7         boolean success; 8         try { 9             if ("..".equals(path)) {10                 changeToParentDirectory();11                 success = true;12             } else {13                 success = client.changeWorkingDirectory(path);14             }15         } catch (IOException e) {16             throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);17         }18         if (!success) {19             throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), "Cannot change directory to: " + path);20         }21     }

     关键就在第13行client.changeWorkingDirectory的方法,而这个client是用的org.apache.commons.net.ftp.FTPClient,上网查找了一下,发现很多人都遇到了FTPClient不能进入中文目录的问题。

     尝试了三种方法:

      第一种改变编码,不行,还是不能进入目录。

     

 uri = new String(uri.getBytes("gb2312"),"iso-8859-1");

   

     第二种方法,设置表头的编码,还是不行。

  

.process(new Processor() {                                                        @Override                            public void process(Exchange exchange) throws Exception {                                exchange.setProperty(Exchange.CHARSET_NAME, "gb2312");                            }                        })

 

   第三种方法,设置FTPClient的编码,这个终于OK了,关键就在最后的红字,ftpClient.controlEncoding=gb2312

   

String uri = "ftp://username@10.10.XX4.122/中文目录?password=xxxx&ftpClient.controlEncoding=gb2312";

 

原创粉丝点击