使用个性化Profile代替Session

系统设计中我们经常要考虑文件上传,但是存在两个让人讨厌的麻烦:

第一:负载均衡

第二:备份

第三:扩展

平常我们是这样上传,使用控件UploadFile,然后saveas就完了,但是这样我们就有上面三个麻烦

如果上传的人很多,服务器硬盘很快满;文件增长很快,我们不好管理,更不好备份很多的上传文件;如果无限制扩展呢?机器的磁盘满了,不限制的加磁盘呢?

自然就想到使用NAS(网络附加存储)或者SAN,如果我们使用NAS或者SAN,哪么就存在一个问题:代码运行环境和文件存储的环境不在一个ASP.NET进程里面,常见有以下两种解决方案:

第一种方法:使用ftp上传,当然这个ftp是编程方式实现

第二种方法:网络共享方式,多见于linux通过mount挂载硬盘

ASP也可以使用这两种方法,这里先介绍使用FTP方式上传,网络共享方式上传,我现在还没有做出成功的DEMO来,FTP已经可以,代码如下

using System;

using System.Collections.Generic;

using System.Text;

using System.Net;

using System.Data;

using System.IO;

using System.ComponentModel;

namespace Common

{

public class FTPClient

{

private string ftpServerIP = String.Empty;

private string ftpUser = String.Empty;

private string ftpPassword = String.Empty;

private string ftpRootURL = String.Empty;

public FTPClient()

{

this.ftpServerIP = @"192.168.202.102";

this.ftpUser = "dxfhly";

this.ftpPassword = "sureme";

this.ftpRootURL = "ASP.NET中使用FTP上传文件 - 网络一小人物 - 网络一小人物的博客ftp://"; + ftpServerIP + "/";

}

/// <summary>

/// 上传

/// </summary>

/// <param name="localFile">本地文件绝对路径</param>

/// <param name="ftpPath">上传到ftp的路径</param>

/// <param name="ftpFileName">上传到ftp的文件名</param>

public bool fileUpload(FileInfo localFile, string ftpPath, string ftpFileName)

{

bool success = false;

FtpWebRequest ftpWebRequest = null;

FileStream localFileStream = null;

Stream requestStream = null;

try

{

string uri = ftpRootURL + ftpPath + ftpFileName;

ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);

ftpWebRequest.UseBinary = true;

ftpWebRequest.KeepAlive = false;

ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;

ftpWebRequest.ContentLength = localFile.Length;

int buffLength = 2048;

byte[] buff = new byte[buffLength];

int contentLen;

localFileStream = localFile.OpenRead();

requestStream = ftpWebRequest.GetRequestStream();

contentLen = localFileStream.Read(buff, 0, buffLength);

while (contentLen != 0)

{

requestStream.Write(buff, 0, contentLen);

contentLen = localFileStream.Read(buff, 0, buffLength);

}

success = true;

}

catch (Exception)

{

success = false;

}

finally

{

if (requestStream != null)

{

requestStream.Close();

}

if (localFileStream != null)

{

localFileStream.Close();

}

}

return success;

}

/// <summary>

/// 上传文件

/// </summary>

/// <param name="localPath">本地文件地址(没有文件名)</param>

/// <param name="localFileName">本地文件名</param>

/// <param name="ftpPath">上传到ftp的路径</param>

/// <param name="ftpFileName">上传到ftp的文件名</param>

public bool fileUpload(string localPath, string localFileName, string ftpPath, string ftpFileName)

{

bool success = false;

try

{

FileInfo localFile = new FileInfo(localPath + localFileName);

if (localFile.Exists)

{

success = fileUpload(localFile, ftpPath, ftpFileName);

}

else

{

success = false;

}

}

catch (Exception)

{

success = false;

}

return success;

}

/// <summary>

/// 下载文件

/// </summary>

/// <param name="localPath">本地文件地址(没有文件名)</param>

/// <param name="localFileName">本地文件名</param>

/// <param name="ftpPath">下载的ftp的路径</param>

/// <param name="ftpFileName">下载的ftp的文件名</param>

public bool fileDownload(string localPath, string localFileName, string ftpPath, string ftpFileName)

{

bool success = false;

FtpWebRequest ftpWebRequest = null;

FtpWebResponse ftpWebResponse = null;

Stream ftpResponseStream = null;

FileStream outputStream = null;

try

{

outputStream = new FileStream(localPath + localFileName, FileMode.Create);

string uri = ftpRootURL + ftpPath + ftpFileName;

ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);

ftpWebRequest.UseBinary = true;

ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;

ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();

ftpResponseStream = ftpWebResponse.GetResponseStream();

long contentLength = ftpWebResponse.ContentLength;

int bufferSize = 2048;

byte[] buffer = new byte[bufferSize];

int readCount;

readCount = ftpResponseStream.Read(buffer, 0, bufferSize);

while (readCount > 0)

{

outputStream.Write(buffer, 0, readCount);

readCount = ftpResponseStream.Read(buffer, 0, bufferSize);

}

success = true;

}

catch (Exception)

{

success = false;

}

finally

{

if (outputStream != null)

{

outputStream.Close();

}

if (ftpResponseStream != null)

{

ftpResponseStream.Close();

}

if (ftpWebResponse != null)

{

ftpWebResponse.Close();

}

}

return success;

}

/// <summary>

/// 重命名

/// </summary>

/// <param name="ftpPath">ftp文件路径</param>

/// <param name="currentFilename"></param>

/// <param name="newFilename"></param>

public bool fileRename(string ftpPath, string currentFileName, string newFileName)

{

bool success = false;

FtpWebRequest ftpWebRequest = null;

FtpWebResponse ftpWebResponse = null;

Stream ftpResponseStream = null;

try

{

string uri = ftpRootURL + ftpPath + currentFileName;

ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);

ftpWebRequest.UseBinary = true;

ftpWebRequest.Method = WebRequestMethods.Ftp.Rename;

ftpWebRequest.RenameTo = newFileName;

ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();

ftpResponseStream = ftpWebResponse.GetResponseStream();

}

catch (Exception)

{

success = false;

}

finally

{

if (ftpResponseStream != null)

{

ftpResponseStream.Close();

}

if (ftpWebResponse != null)

{

ftpWebResponse.Close();

}

}

return success;

}

/// <summary>

/// 消除文件

/// </summary>

/// <param name="filePath"></param>

public bool fileDelete(string ftpPath, string ftpName)

{

bool success = false;

FtpWebRequest ftpWebRequest = null;

FtpWebResponse ftpWebResponse = null;

Stream ftpResponseStream = null;

StreamReader streamReader = null;

try

{

string uri = ftpRootURL + ftpPath + ftpName;

ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);

ftpWebRequest.KeepAlive = false;

ftpWebRequest.Method = WebRequestMethods.Ftp.DeleteFile;

ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();

long size = ftpWebResponse.ContentLength;

ftpResponseStream = ftpWebResponse.GetResponseStream();

streamReader = new StreamReader(ftpResponseStream);

string result = String.Empty;

result = streamReader.ReadToEnd();

success = true;

}

catch (Exception)

{

success = false;

}

finally

{

if (streamReader != null)

{

streamReader.Close();

}

if (ftpResponseStream != null)

{

ftpResponseStream.Close();

}

if (ftpWebResponse != null)

{

ftpWebResponse.Close();

}

}

return success;

}

/// <summary>

/// 文件存在检查

/// </summary>

/// <param name="ftpPath"></param>

/// <param name="ftpName"></param>

/// <returns></returns>

public bool fileCheckExist(string ftpPath, string ftpName)

{

bool success = false;

FtpWebRequest ftpWebRequest = null;

WebResponse webResponse = null;

StreamReader reader = null;

try

{

string url = ftpRootURL + ftpPath;

ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));

ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);

ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;

ftpWebRequest.KeepAlive = false;

webResponse = ftpWebRequest.GetResponse();

reader = new StreamReader(webResponse.GetResponseStream());

string line = reader.ReadLine();

while (line != null)

{

if (line == ftpName)

{

success = true;

break;

}

line = reader.ReadLine();

}

}

catch (Exception)

{

success = false;

}

finally

{

if (reader != null)

{

reader.Close();

}

if (webResponse != null)

{

webResponse.Close();

}

}

return success;

}

}

}

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

using System.Net;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

/*

//建立目录

FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ASP.NET中使用FTP上传文件 - 网络一小人物 - 网络一小人物的博客ftp://192.168.202.102/dreamworld/test";);

Request.Credentials = new NetworkCredential("dxfhly", "sureme");

Request.Method = WebRequestMethods.Ftp.MakeDirectory;

Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘

FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();

Response.Close();

//删除目录

Request = (FtpWebRequest)WebRequest.Create("ASP.NET中使用FTP上传文件 - 网络一小人物 - 网络一小人物的博客ftp://192.168.202.102/dreamworld/test";);

Request.Credentials = new NetworkCredential("dxfhly", "sureme");

Request.Method = WebRequestMethods.Ftp.RemoveDirectory;

Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘

Response = (FtpWebResponse)Request.GetResponse();

Response.Close();

*/

/*

//修改文件名

Request = (FtpWebRequest)WebRequest.Create("ASP.NET中使用FTP上传文件 - 网络一小人物 - 网络一小人物的博客ftp://192.168.202.102/dreamworld/ssa.txt";);

Request.Credentials = new NetworkCredential("dxfhly","sureme");

Request.Method = WebRequestMethods.Ftp.Rename;

Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘

Request.RenameTo = "NewName.txt";

Response = (FtpWebResponse)Request.GetResponse();

Response.Close();

//修改目录名

Request = (FtpWebRequest)WebRequest.Create("ASP.NET中使用FTP上传文件 - 网络一小人物 - 网络一小人物的博客ftp://192.168.202.102/dreamworld/test";);

Request.Credentials = new NetworkCredential("dxfhly","sureme");

Request.Method = WebRequestMethods.Ftp.Rename;

Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘

Request.RenameTo = "test2";

Response = (FtpWebResponse)Request.GetResponse();

Response.Close();

//删除文件

Request = (FtpWebRequest)WebRequest.Create("ASP.NET中使用FTP上传文件 - 网络一小人物 - 网络一小人物的博客ftp://192.168.202.102/dreamworld/NewName.txt";);

Request.Credentials = new NetworkCredential("dxfhly","sureme");

Request.Method = WebRequestMethods.Ftp.DeleteFile;

Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘

Response = (FtpWebResponse)Request.GetResponse();

Response.Close();

//上传文件

Request = (FtpWebRequest)WebRequest.Create("ASP.NET中使用FTP上传文件 - 网络一小人物 - 网络一小人物的博客ftp://192.168.202.102/dreamworld/";);

Request.Credentials = new NetworkCredential("dxfhly","sureme");

Request.Method = WebRequestMethods.Ftp.UploadFile;

Stream sourcestream=

sourcestream= Request.GetRequestStream();

Request.Timeout = (60000 * 1); //60000 * 1,表示1分鐘

Response = (FtpWebResponse)Request.GetResponse();

Response.Close();

*/

}

protected void Button1_Click(object sender, EventArgs e)

{

// FileUpload1.SaveAs(@"\\192.168.202.42\testshare");

//FileUpload1.SaveAs(@"Z:\test");

//上传文件

Common.FTPClient ftpclient = new Common.FTPClient();

FileInfo finfo = new FileInfo(@"F:\dxf\BIND9.3.2-P2.nt4.rar");

ftpclient.fileUpload(finfo, "/dreamworld/", "BIND9.3.2-P2.nt4.rar");

}

}

你可以根据你的实际需要修改一下,就可以使用ftp方式上传文件了

转载于:https://my.oschina.net/dxf/blog/272

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值