C# linux FTP client [客服端]
由于工作需要,做了一个C#的FTP 客户端,FTP服务端在linux服务器上面,测试了.NET 平台自带的FTP类,只是上传了的文件(图片或者html页面)不能通过浏览器浏览.
一开始以为是编码的问题,但是通过linux 的VIM 编辑器打开也没有发现乱码的问题,所有可以排除是编码格式不对,因为统一采用的是二进制的传输和保持方式。
后来以为是gif 图片在linux和windows 上处理方式不一样,通过查资料排除了这种可能性
//==========================================
直到刚刚才弄明白,原来是上传的文件权限不够(r--rw--w-),如果将权限设置为最低(r--rw-rw-)即可。呵呵,问题解决了可以睡觉了!有时候问题就你最不经意的地方!
今天在Silverlight群里面和大家一起探讨了一些问题,受益颇多。
很想买一根内存条,现在2G的内存跑了Win7后就不能干啥的了,虚拟机用起来卡死了,如果开了VS 2008,VS 2010,SQL 2005 就崩溃了。连Dreamweaver CS3 开了都占30%左右的内存。
/// <summary>
/// 图片浏览
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开(Open)";
ofd.FileName = "";
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//为了获取特定的系统文件夹,可以使用System.Environment类的静态方法GetFolderPath()。该方法接受一个Environment.SpecialFolder枚举,其中可以定义要返回路径的哪个系统目录
ofd.Filter = "CSV文件(*.jpg)|*.jpg";
ofd.ValidateNames = true; //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名
ofd.CheckFileExists = true; //验证路径有效性
ofd.CheckPathExists = true; //验证文件有效性
if (ofd.ShowDialog() == DialogResult.OK)
{
filepath = ofd.FileName.Substring(0, ofd.FileName.Length - ofd.SafeFileName.Length - 1);
filepath = filepath.Replace("/", @"\\");
filename = ofd.SafeFileName;
textBox4.Text = filepath + @"\" + filename;
}
}
/// <summary>
/// FTP 上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click_1(object sender, EventArgs e)
{
file_name_url_image_d = "mili" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".jpg";
FtpStatusCode status = UploadFun(textBox4.Text, "ftp://203.86.26.190/milimall/images_d/images/upload/" + file_name_url_image_d);
FtpStatusCode status2 = UploadFun(textBox4.Text, "ftp://203.86.26.190/milimall/images/upload/Image/" + file_name_url_image_d);
if (status.ToString() == "ClosingData" && status2.ToString() == "ClosingData")
{
MessageBox.Show("上传成功");
MessageBox.Show("ftp://203.86.26.190/milimall/images_d/images/upload/Image/" + file_name_url_image_d);
mark_upload = true;
}
else
{
file_name_url_image_d = "";
MessageBox.Show("上传失败");
}
}
#region FTP 上传类
/// <summary>
/// FTP 上传类
/// </summary>
/// <param name="fileName"></param>
/// <param name="uploadUrl"></param>
/// <returns></returns>
private FtpStatusCode UploadFun(string fileName, string uploadUrl)
{
Stream requestStream = null;
FileStream fileStream = null;
FtpWebResponse uploadResponse = null;
try
{
FtpWebRequest uploadRequest =
(FtpWebRequest)WebRequest.Create(uploadUrl);
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
uploadRequest.Proxy = null;
NetworkCredential nc = new NetworkCredential();
//nc.Domain = "203.86.26.190";
nc.UserName = "milimall";
nc.Password = "wesdxc91";
// uploadRequest.UseBinary = true;
uploadRequest.Credentials = nc; //修改getCredential();错误2
requestStream = uploadRequest.GetRequestStream();
fileStream = File.Open(fileName, FileMode.Open);
byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Close();
uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
return uploadResponse.StatusCode;
}
catch (UriFormatException ex)
{
}
catch (IOException ex)
{
}
catch (WebException ex)
{
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
return FtpStatusCode.Undefined;
}
#endregion