使用个性化Profile代替Session
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
系統(tǒng)設計中我們經(jīng)常要考慮文件上傳,但是存在兩個讓人討厭的麻煩:
第一:負載均衡
第二:備份
第三:擴展
平常我們是這樣上傳,使用控件UploadFile,然后saveas就完了,但是這樣我們就有上面三個麻煩
如果上傳的人很多,服務器硬盤很快滿;文件增長很快,我們不好管理,更不好備份很多的上傳文件;如果無限制擴展呢?機器的磁盤滿了,不限制的加磁盤呢?
自然就想到使用NAS(網(wǎng)絡附加存儲)或者SAN,如果我們使用NAS或者SAN,哪么就存在一個問題:代碼運行環(huán)境和文件存儲的環(huán)境不在一個ASP.NET進程里面,常見有以下兩種解決方案:
第一種方法:使用ftp上傳,當然這個ftp是編程方式實現(xiàn)
第二種方法:網(wǎng)絡共享方式,多見于linux通過mount掛載硬盤
ASP也可以使用這兩種方法,這里先介紹使用FTP方式上傳,網(wǎng)絡共享方式上傳,我現(xiàn)在還沒有做出成功的DEMO來,FTP已經(jīng)可以,代碼如下
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 = "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("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("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("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("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("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("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");
}
}
你可以根據(jù)你的實際需要修改一下,就可以使用ftp方式上傳文件了
轉(zhuǎn)載于:https://my.oschina.net/dxf/blog/272
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的使用个性化Profile代替Session的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JEECG 命名规范
- 下一篇: 日语 五十音图