日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用个性化Profile代替Session

發布時間:2025/3/16 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用个性化Profile代替Session 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

系統設計中我們經常要考慮文件上傳,但是存在兩個讓人討厭的麻煩:

第一:負載均衡

第二:備份

第三:擴展

平常我們是這樣上傳,使用控件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 = "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");

}

}

你可以根據你的實際需要修改一下,就可以使用ftp方式上傳文件了

轉載于:https://my.oschina.net/dxf/blog/272

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的使用个性化Profile代替Session的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 懂色av一区二区三区 | 少妇久久久久久被弄到高潮 | 久久av资源| 少妇久久久久久久久久 | 国产高清在线免费观看 | 国产精自产拍久久久久久蜜 | 国产成人无码精品亚洲 | 亚洲成人高清在线 | 久久短视频 | 视频一区二区不卡 | 很色的网站| 中文一区在线观看 | 五月的婷婷| 免费观看日本 | 91免费国产在线 | 国产另类精品 | 日韩欧美亚洲在线 | japansexxxxhd医生 夜夜操导航 | 毛片视频免费 | 麻豆一区产品精品蜜桃的特点 | 天天搞夜夜爽 | 色伊人影院 | 日本三级视频网站 | 精品乱码一区内射人妻无码 | 国产白丝在线观看 | 黑人玩弄人妻一区二区三区免费看 | 日韩中文第一页 | 黑人专干日本人xxxx | 久草免费在线播放 | 日本一区二区三区在线看 | 成人爽站w47pw | 一本大道久久a久久精二百 琪琪色在线视频 | 秋霞在线视频 | 五月婷婷中文字幕 | 色999在线 | 少妇的被肉日常np | 精品一区二区在线免费观看 | 天天操天天操天天干 | 插插插91 | 健身教练巨大粗爽gay视频 | 日韩精品一区二区三区免费视频 | 艳妇乳肉亭妇荡乳av | 激情视频在线观看免费 | 一区二区三区人妻 | 婷婷色婷婷 | 91在线观看视频 | 国产福利影院 | 九九热最新视频 | 国产大学生自拍视频 | 91精彩视频在线观看 | 在线污视频| 亚欧av在线| 亚洲免费在线视频观看 | 黑人乱码一区二区三区av | 夜夜操影院| 日韩欧美在线播放 | 一区二区日韩欧美 | 国产成人精品一区在线播放 | 午夜国产一区二区三区 | 久久潮| 亚洲天堂成人网 | 午夜黄色影院 | 国产精品探花一区二区在线观看 | 少妇裸体性生交 | 国产三级国产精品国产专区50 | 欧美成人精品一区二区免费看片 | 午夜快播 | 操穴网站| 色多多视频网站 | 韩国三级在线播放 | 特黄视频| 亚洲国产精品成人va在线观看 | 欧美一区二区三区黄色 | 国产绿帽一区二区三区 | 亚洲一区二区欧美 | 每日av在线 | 你懂的国产在线 | 91精品国产自产精品男人的天堂 | www激情com | 日韩视频在线观看免费 | 色偷偷人人澡人人爽人人模 | 国产娇小hdxxxx乱 | 黄视频免费在线看 | 午夜激情啪啪 | 少妇人妻偷人精品无码视频 | 成人午夜在线视频 | 色妞综合网 | 高清国产视频 | 天天操人人干 | 黄色片网站免费在线观看 | 波多野结衣久久 | 日韩免费观看一区二区 | 网友自拍一区 | 东京av在线 | 日韩一级在线视频 | av中文在线 | 亚洲精品视频专区 | 国产免费一区二区三区在线观看 | 日本免费一区二区三区 |