使用NET內建FTP出現中文檔名變亂碼

本來興高采烈的寫出NET 上傳下載的FTP後測試一切正常,結果在最後階段發現中文檔名出現亂碼(內文正常),查了老半天因為能力不足無法解決,最後只好靠不太會操作的 win32API終於解決了中文檔名的問題,不過也折騰了好幾天,真是要多加油了><。 NET 的FTP改成可以多個檔案上傳並且再最後一的檔案時關閉FTP連線使用ftpReq.KeepAlive = true; NET竟然沒有CLOSE 所以我也沒辦法測試這樣是否真的關閉,至於API部分,由於目前台灣時間是早上五點半,在不睡就要死了,所以改天有機會再寫了

上傳檔案部份
public void FileUpload(string[] arryFileName)
{
    Stream requestStream = null;
    FileStream fileStream = null;
    FtpWebResponse ftpRep = null;
    string strFileName;
    int intAryLen = arryFileName.GetLength(0);

    for (int i = 0; i < intAryLen; i++)
    {
        try
        {
            strFileName = System.IO.Path.GetFileName(arryFileName[i]);
            FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create("ftp://FTPHost/檔案名稱");
            ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
            ftpReq.Proxy = null; ftpReq.UsePassive = false;
            ftpReq.Credentials = new NetworkCredential("FTPUserID", "FTPPwd");
            ftpReq.UseBinary = true;

            if (i == intAryLen - 1)
            {
                //最後一個檔案時關閉 
                ftpReq.KeepAlive = true;
            }
            else
            {
                ftpReq.KeepAlive = false;
            }

            requestStream = ftpReq.GetRequestStream();
            fileStream = File.Open(arryFileName[i], FileMode.Open);
            byte[] buffer = new byte[2048]; int intbytes;

            while (true)
            {
                intbytes = fileStream.Read(buffer, 0, buffer.Length);
                if (intbytes == 0) break;

                requestStream.Write(buffer, 0, intbytes);
            }

            requestStream.Close();
            ftpRep = (FtpWebResponse)ftpReq.GetResponse();
        }
        catch (Exception ex) { }
        finally
        {
            if (ftpRep != null) ftpRep.Close(); 
            if (fileStream != null) fileStream.Close(); 
            if (requestStream != null) requestStream.Close();
        }
    }
}


下載檔案部份
public void FileDownload(string[] arryFileName)
{
    string strSavePath = Application.StartupPath + "\\DownloadBuf\\";
    Stream responseStream = null;
    FileStream fileStream = null;
    StreamReader reader = null;
    string strFileName = "";
    int intAryLen = arryFileName.GetLength(0);
    FtpWebResponse ftpRep = null;

    for (int i = 0; i < intAryLen; i++)
    {
        try
        {
            strFileName = System.IO.Path.GetFileName(arryFileName[i]);
            FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create("ftp://FTPHost名稱/檔案名稱");
            ftpReq.Method = WebRequestMethods.Ftp.DownloadFile;
            ftpReq.Credentials = new NetworkCredential("FTPUserID", "FTPPwd");
            ftpReq.UseBinary = true;

            if (i == intAryLen - 1)
            {
                ftpReq.KeepAlive = true;//最後一個檔案時關閉 
            }
            else
            {
                ftpReq.KeepAlive = false;
            }
            ftpRep = (FtpWebResponse)ftpReq.GetResponse();
            responseStream = ftpRep.GetResponseStream();//取得FTP伺服器回傳的資料流 

            string fileName = Path.GetFileName(ftpReq.RequestUri.AbsolutePath);
            if (fileName.Length == 0)
            {
                reader = new StreamReader(responseStream);
                throw new Exception(reader.ReadToEnd());
            }
            else
            {
                fileStream = File.Create(strSavePath + fileName);
                byte[] buffer = new byte[1024];
                int bytesRead;

                while (true)
                {
                    bytesRead = responseStream.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0) break;
                    fileStream.Write(buffer, 0, bytesRead);
                }
            }
        }
        catch (IOException ex) { }
        finally
        {
            if (ftpRep != null) ftpRep.Close();
            if (reader != null)
            {
                reader.Close();
            }
            else if (responseStream != null)
            {
                responseStream.Dispose();
                responseStream.Close();
            }
            if (fileStream != null) fileStream.Close();
        }
    }
}


查詢遠端檔案部份
StreamReader reader = null;
try
{
    FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create("ftp://FTPHost名稱/");
    ftpReq.Credentials = new NetworkCredential("FTPUserID", "FTPPwd");
    ftpReq.Method = WebRequestMethods.Ftp.ListDirectory;// //設定Method取得簡短目錄資訊
    //ftpReq.Method = WebRequestMethods.Ftp.ListDirectoryDetails;// //設定Method取得詳細目錄資訊
    ftpReq.UsePassive = false;
    FtpWebResponse ftpRep = (FtpWebResponse)ftpReq.GetResponse();

    reader = new StreamReader(ftpRep.GetResponseStream(), System.Text.Encoding.Default);

    //開始讀取回傳資訊
    while (reader.Peek() >= 0)
    {
        string[] arryBuf = reader.ReadLine().Split(' ');
        string strNewBuf = "";
        foreach (string strBuf in arryBuf)
        {
            if (strBuf.Trim() == "")
            {
                strNewBuf = "@@@";
            }
            else
            {
                strNewBuf = strBuf;
            }
            //listBox1.Items.Add(strNewBuf);
        }

    }
}
catch (Exception ex) { }
finally
{
    if (reader != null) reader.Close();
}

留言