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

 

轉:html

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

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

有臺服務器,編碼爲GBK,發現服務器上的中文文件下載後文件大小爲0,打開爲空白。java

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

 
  具體代碼: 
 
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();
            }
        }
 
    }
 
}
相關文章
相關標籤/搜索