如何从ftp服务器下载binarry文件 [英] how can download binarry files from ftp server

查看:130
本文介绍了如何从ftp服务器下载binarry文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.
请原谅我的英语
我在VS.net 2008中有问题.

我想从ftp服务器下载文件,但下载的文件不是utf8,波斯语单词无法正确显示.

hi.
excuse me for my english
i have a problem in VS.net 2008.

i want download a file from a ftp server but downloaded file is not utf8 and farsi word dont show correctly. how can download it?

推荐答案

如果使用可用的标准FTP客户端,则设置二进制模式所需的命令为binary:
http://www.cs.colostate.edu/helpdocs/ftp.html [ ^ ].

如果使用.NET进行FTP客户端操作,则很可能使用System.Net.FtpWebRequest,则需要使用属性System.Net.FtpWebRequest.UseBinary:
http://msdn.microsoft.com/en-us/library/system. net.ftpwebrequest.usebinary.aspx [ ^ ].

实际上,我建议在所有情况下都使用二进制模式.

另外,您确定还不是UTF-8吗?例如,如果可能是不带BOM的UTF-8,它可能会首先使用其他一些UTF,依此类推.您可以尝试使用不同的编码读取文本,并使用所需的编码和BOM编写文本.

请参阅:
http://en.wikipedia.org/wiki/Byte_order_mark [ http://unicode.org/faq/utf_bom.html [
If you use available and standard FTP client, the required command to set the binary mode is binary:
http://www.cs.colostate.edu/helpdocs/ftp.html[^].

If you do FTP client-side operations using .NET, most likely you use System.Net.FtpWebRequest, then you need to use the property System.Net.FtpWebRequest.UseBinary:
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usebinary.aspx[^].

In practice, I would advise to use binary mode in all cases.

Also, are you sure it''s not UTF-8 already? If could be, say, UTF-8 without BOM, it could use some other UTF in first place, etc. You can try to read the text using different encodings and write using the encoding you need and with BOM.

Please see:
http://en.wikipedia.org/wiki/Byte_order_mark[^],
http://unicode.org/faq/utf_bom.html[^].

One quick way of checking up the encoding is using some Web browser. Browsers usually have something like View -< Character Encoding options which would allow you to try them, with a plain text file or HTML.

—SA


UTF8是文本编码.如果要下载二进制文件,为什么还要指定编码?您不需要它.
UTF8 is a text encoding. If you''re downloading binary files, why are you even specifying an encoding?? You don''t need it.


如果UseBinary = true可以下载二进制文件,而在donwnload过程中更改较少,如下所示:


私有无效GetFile(String strPAth,String strUN,String strPass,String strSavePath)
{
试试
{
FtpWebRequest equest = FtpWebRequest)FtpWebRequest.Create(strPAth);
Request.Credentials =新的NetworkCredential(strUN,strPass);
Request.KeepAlive = false;
Request.UseBinary = true;
Request.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse Resp =(FtpWebResponse)Request.GetResponse();
流RS = Resp.GetResponseStream();
FileStream FS =新的FileStream(strSavePath,FileMode.Create);
Byte [] buf = new Byte [2047];
Int32读取= 0;

{
读取= RS.Read(buf,0,buf.Length);
FS.Write(buf,0,Read);
} while(Read!= 0);
RS.Close();
FS.Flush();
FS.Close();
Resp.Close();
Request.Abort();
}
赶上
{

}
}
if UseBinary = true can Download Binary file with less changes in donwnload process like this:


private void GetFile(String strPAth,String strUN,String strPass,String strSavePath)
{
try
{
FtpWebRequest equest=FtpWebRequest)FtpWebRequest.Create(strPAth);
Request.Credentials = new NetworkCredential(strUN, strPass);
Request.KeepAlive = false;
Request.UseBinary = true;
Request.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse Resp = (FtpWebResponse)Request.GetResponse();
Stream RS= Resp.GetResponseStream();
FileStream FS = new FileStream(strSavePath, FileMode.Create);
Byte[] buf = new Byte[2047];
Int32 Read=0;
do
{
Read = RS.Read(buf, 0, buf.Length);
FS.Write(buf, 0, Read);
} while (Read != 0);
RS.Close();
FS.Flush();
FS.Close();
Resp.Close();
Request.Abort();
}
catch
{

}
}


这篇关于如何从ftp服务器下载binarry文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆