日韩性视频-久久久蜜桃-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的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 国产精品久久久久久久久久久新郎 | 日本3级网站 | 日韩丰满少妇无码内射 | 日本高清视频网站 | 91免费版黄 | 中文一二区 | 久福利 | 久久久伦理片 | 日本中文字幕在线看 | 欧美一区二区在线播放 | 精品人伦一区二区 | 美女涩涩视频 | 欧美专区日韩专区 | 911国产 | 国产情侣av自拍 | 成人xxxx | 丰满少妇一区二区三区 | 污视频网站在线播放 | 国产激情自拍视频 | 欧美整片在线观看 | 国产精品一级黄色片 | 中国a一片一级一片 | 欧美偷拍少妇精品一区 | 免费裸体视频网站 | 激情五月婷婷丁香 | 风间由美一区二区三区 | 色婷婷在线观看视频 | 师生出轨h灌满了1v1 | 有声小说 成人专区 | 国产精品久久久久久久久久妞妞 | 亚洲欧洲日韩国产 | 欧美性生交xxxxxdddd | 免费精品国产 | 奇米影视久久 | 亚洲一区二区影视 | 超碰夫妻 | 黄色免费毛片 | 91亚洲国产成人精品一区 | 国产免费一区二区三区在线播放 | 人人妻人人澡人人爽欧美一区双 | 欧美精品久久96人妻无码 | jzzijzzij亚洲成熟少妇在线播放 狠狠躁日日躁夜夜躁2022麻豆 | 人人妻人人玩人人澡人人爽 | 黄色大片免费观看 | 亚洲一二三四视频 | 永久免费在线播放 | 美女交配 | 国产美女主播视频 | 老熟妇高潮一区二区高清视频 | 2024国产精品 | 国产毛片久久久久 | 国产午夜福利100集发布 | 国产成人久久精品 | 欧美日韩一卡二卡三卡 | 欧美在线视频你懂的 | 久久久一级黄色片 | 亚洲欧美日韩一区在线观看 | 国产乱欲视频 | 性感美女福利视频 | 午夜视频一区二区 | 精品国产av一区二区 | 欧美三级黄| 免费毛片网站在线观看 | 国产精品久线在线观看 | 九色视频自拍 | 黑人操亚洲美女 | 欧洲精品久久久久毛片完整版 | 日本一区二区三区视频在线播放 | 黄色网战大全 | 告诉我真相俄剧在线观看 | 欧美在线免费视频 | 国产视频一区二区在线播放 | 97免费观看视频 | 一级a性色生活片久久毛片 爱爱高潮视频 | 西西444www大胆无视频 | 日本 在线| 亚洲精品二 | 视频免费在线观看 | 福利一二三区 | 一个人在线免费观看www | 国产伦精品一区二区三区视频孕妇 | 狠狠人妻久久久久久 | 欧美 日韩 人妻 高清 中文 | 日韩欧美一区二区三区四区五区 | 国产精成人品免费观看 | av国产精品| 182tv福利视频 | 蜜桃视频成人在线观看 | 国产精品视频成人 | 天天综合中文字幕 | 欧美色爽 | 亚洲中文字幕无码一区二区三区 | 99热国内精品 | 狠狠撸在线视频 | 日韩爱爱网站 | 亚洲伊人网站 | 越南性xxxx精品hd | 国产精品久久久久久中文字 | 熟女视频一区二区三区 |