解決FTP服務器上中文名文件下載后為空的問題


 

轉:

解決FTP服務器上中文名文件下載后為空的問題

 版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/zy910525/article/details/75530068

有台服務器,編碼為GBK,發現服務器上的中文文件下載后文件大小為0,打開為空白。

經調查,是文件名編碼格式不對導致,對於中文情況,使用FTPClient時編碼格式需使用ISO-8859-1

具體代碼:
package com.neusoft.ftptest;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
 
public class FtpMain {
 
    public static void main(String[] args) {
        FTPClient client = new FTPClient();
        try {
            client.connect("10.10.xxx.xxx", 21);
            client.login("administrator", "xxx");
            System.out.println(client.getControlEncoding());
            int reply = client.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                client.disconnect();
                System.out.println("Login error");
                return;
            }
            client.setControlEncoding("GBK");
            // client.segt
 
            System.out.println(client.getCharsetName());
            // client.enterRemotePassiveMode();
            client.enterLocalPassiveMode();
            client.changeWorkingDirectory("11_COMMUNICATION/201204");
 
            System.out.println("---------------------------------------");
 
            String[] names;
 
            names = client.listNames();
            for (int i = 0; i < names.length; i++) {
                System.out.println(names[i]);
            }
            System.out.println(names.toString());
 
            System.out.println("---------------------------------------");
 
            FTPFile f = client.listFiles()[0];
            System.out.println(f.getLink());
            client.changeWorkingDirectory("/");
            String path = "/10_NOTICE_FILE/201706";
            // String path = "/10_NOTICE_FILE/201203/";
 
            client.setBufferSize(1024);
            client.setFileType(FTP.BINARY_FILE_TYPE);
            client.enterLocalPassiveMode();
            client.changeWorkingDirectory(path);
 
            FTPFile[] fs = client.listFiles();
            FileOutputStream out = null;
            InputStream in = null;
            for (int i = 0; i < fs.length; i++) {
                FTPFile ff = fs[i];
                String outFileName = ff.getName();
                System.out.println(outFileName);
 
                //本地目錄文件不需要編碼
                File localFile = new File("D:\\ftp\\" + ff.getName());
                OutputStream fos = new FileOutputStream(localFile);
                // ftp需使用ISO-8859-1編碼格式
                String localFileName = new String(ff.getName().getBytes("GBK"), "ISO-8859-1");
                client.retrieveFile(localFileName, fos);
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                client.disconnect();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
 
    }
 
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM