FTPClient 上传中文名称文件到windows系统上的ftp服务器乱码问题

2 篇文章 0 订阅
1 篇文章 0 订阅

1:使用org.apache.commons.net.ftp.FTPClient

2:调整后的上传文件java代码:

public boolean uploadTxtFile(String filename, String r,
            String tempftpDirectory) {
        boolean success = false;
        InputStream is = null;
        try {
        	String LOCAL_CHARSET="GBK";
        	if (FTPReply.isPositiveCompletion(this.ftpClient.sendCommand("OPTS UTF8", "ON"))) {
        		LOCAL_CHARSET = "UTF-8";
        	}
        	this.ftpClient.setControlEncoding(LOCAL_CHARSET);
//        	上传文件时,文件名称需要做编码转换
        	String name = new String(filename.getBytes(LOCAL_CHARSET),"ISO-8859-1");
//        	System.err.println("setControlEncoding编码:"+LOCAL_CHARSET+"-----转码后的文件名称:"+fileName);
            // 1.输入流
            is = new ByteArrayInputStream(r.getBytes("utf-8"));
            this.ftpClient.changeWorkingDirectory(tempftpDirectory);// 改变工作路径
            success = this.ftpClient.storeFile(name, is);
            if (success == true) {
                return success;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }

 

3:如图文件名称乱码

4:将文件从windows搭建的ftp服务器取下来存放到linux下(windows下搭建服务器使用的是Gene6 FTP Server)

5:执行linux命令:convmv -r -f utf8 -t iso88591 * --notest --nosmart && convmv -r -f gbk -t utf8 * --notest --nosmart

6:这个命令需要安装 yum install convmv

7:结果如图:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值