FTPClient 上傳中文名稱文件到windows系統上的ftp服務器亂碼問題

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:結果如圖:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章