在本次开发中,向服务器上传文件,使用二进制流形式上传至服务器。本因为异步,还添加了多线程,虽然用起来没问题,但是有很多不足之处。

public class FtpHelper
{
#region 属性
string ftpUserName;
string ftpServerIP;
string ftpPassword;
string ftpRemotePath;
#endregion

public FtpHelper()
{
//读取配置文件
this.ftpUserName = GetSetting<string>("FtpUserName", "");
this.ftpPassword = GetSetting<string>("FtpPassWord", "");
this.ftpServerIP = GetSetting<string>("ftpServerIP", "");
this.ftpRemotePath = "ftp://" + ftpServerIP + "/";
}

//const string mutextName = "wwwwwwwwhhhw0000lllwwww";
//Mutex mt = new Mutex(true, mutextName);
public string FtpUserName
{
get { return GetSetting<string>("FtpUserName", ""); }
}
public string FtpPassWord
{
get { return GetSetting<string>("FtpPassWord", ""); }
}
public string FtpServerIP
{
get { return GetSetting<string>("ftpServerIP", ""); }
}
private int flag = 0;
private MainForm mainForm = null;

public FtpHelper(MainForm _mainform)
{
this.mainForm = _mainform;
}

public void fileupload()
{

string excelMx = "《日外呼营销明细报表" + DateTime.Now.Year + "年" + DateTime.Now.Month + "月》.xls";
string excelRb = "湖北交通顾问日报表" + DateTime.Now.Year + "." + DateTime.Now.Month + ".xls";
string filename = "jwyjl" + DateTime.Now.ToString("yyyyMMdd") + ".txt";

FtpTheading fo0 = null;
fo0=new FtpTheading(excelMx);
fo0.StartUpLoadEvent += new FtpTheading.UploadEventsHandler(this.FileUpload); //注册事件
fo0.ThreadStart(); //调用类的方法

FtpTheading fo1 = new FtpTheading(excelRb);
fo1.StartUpLoadEvent += new FtpTheading.UploadEventsHandler(this.FileUpload); //注册事件
fo1.ThreadStart(); //调用类的方法

FtpTheading fo2 = new FtpTheading(filename);
fo2.StartUpLoadEvent += new FtpTheading.UploadEventsHandler(this.FileUpload); //注册事件
fo2.ThreadStart(); //调用类的方法

}


public void FileUpload(string fileName)
{
//mt.WaitOne();
//Interlocked.Increment(ref flag);
FileInfo fi = new FileInfo(fileName);
try
{
if (fi.Exists)
{
WebClient request = new WebClient();
//注册完成事件,以便上传完成时,收到通知
//mainForm.SetText(GetSetting<string>("FtpUserName", ""));
request.UploadDataCompleted += new UploadDataCompletedEventHandler(request_UploadDataCompleted);
request.Credentials = new NetworkCredential(FtpUserName, FtpPassWord);
FileStream myStream = new FileStream(Application.StartupPath + "\\" + fileName, FileMode.Open, FileAccess.Read);
byte[] dataByte = new byte[myStream.Length];
myStream.Read(dataByte, 0, dataByte.Length); //写到2进制数组中
myStream.Close();
Uri uri = new Uri("ftp://" + FtpServerIP + "/" + fileName);
request.UploadDataAsync(uri, "STOR", dataByte, dataByte);
mainForm.SetText(DateTime.Now.ToString() + "-->成功上传:" + fileName + "\r\n");
}
else
{
mainForm.SetText(DateTime.Now.ToString() + "-->文件" + fileName + "不存在\r\n");
}
}
catch (Exception ex)
{
mainForm.SetText("上传文件时出现:" + ex.Message + "\r\n");
}
finally
{

}
//Interlocked.Decrement(ref flag);
//mt.ReleaseMutex();//释放线程
}
/*
* ftp上传回调函数
*
*/
#region
void request_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
{
//接收UploadDataAsync传递过来的用户定义对象
byte[] dataByte = (byte[])e.UserState;

//AsyncCompletedEventArgs.Error属性,获取一个值,该值指示异步操作期间发生的错误
if (e.Error == null)
{

//MessageBox.Show(string.Format("上传成功!文件大小{0}", dataByte.Length));
}
else
{
mainForm.SetText(e.Error.Message);
}
}
#endregion


//提取配置文件信息
public T GetSetting<T>(String key, T defaultValue)
{
if (System.Configuration.ConfigurationSettings.AppSettings[key] == null)
return defaultValue;

//mailsetting is not a string, therefore we need to use websetting.
if (typeof(T) == typeof(String))
return (T)(object)System.Configuration.ConfigurationSettings.AppSettings[key];
if (typeof(T) == typeof(int))
return (T)(object)Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings[key]);
if (typeof(T) == typeof(bool))
return (T)(object)(bool.Parse(System.Configuration.ConfigurationSettings.AppSettings[key]));
return (T)(object)System.Configuration.ConfigurationSettings.AppSettings[key];
}
}

 

辅助类文件

public class FtpTheading
{
public string FineName { get; set; }
public delegate void UploadEventsHandler(string _filename);
public event UploadEventsHandler StartUpLoadEvent;
public FtpTheading()
{
}
/// <summary>
///
/// </summary>
/// <param name="_filename">上传的文件名</param>
public FtpTheading(string _fileName)
{
this.FineName = _fileName;
}
/// <summary>
/// 开始一个线程,执行事件
/// </summary>
public void ThreadStart()
{
Thread thr = new Thread(new ThreadStart(this.Start));
thr.Start();
}
/// <summary>
/// 开始事件
/// </summary>
public void Start()
{
StartUpLoadEvent(this.FineName);
}
}

 

其中遇到一些问题,比如我打开excel文件写入很多内容后,在多线程的情况下强制关闭窗口后如何杀死当前线程启动的所有excel进程?

posted on 2013-05-16 17:37  逐梦☆飞扬  阅读(582)  评论(0编辑  收藏  举报