文档章节

使用个性化Profile代替Session

邓小峰
 邓小峰
发布于 2009/03/26 20:48
字数 1322
阅读 621
收藏 0

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

第一:负载均衡

第二:备份

第三:扩展

平常我们是这样上传,使用控件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方式上传文件了

© 著作权归作者所有

共有 人打赏支持
邓小峰
粉丝 26
博文 38
码字总数 17272
作品 0
广州
高级程序员
私信 提问
[前端] express做登录 研究过程

Node Express获取参数的几种方式 http://xuyuan923.github.io/2014/10/10/node-tutorial-req/ EJS模板引擎 http://blog.csdn.net/zhangxin09/article/details/18409119 云引擎示例 https://......

丁一鸣-前端
2016/05/12
3
0
Ember.js 入门指南——自定义适配器

在Ember应用中适配器决定了数据保存到后台的方式,比如URL格式和请求头部。Ember Data默认的适配器是内置的REST API回调。 实际使用中经常会扩展默认的适配器。Ember的立场是应该通过扩展适配...

ubuntuvim
2015/10/23
239
0
ORA-02396: exceeded maximum idle time, please connect again的原因

一般为了防止过多活动的session占用资源,可以对允许连接到数据库的session个数,已连接到数据库的session空闲时间等进行限制(当然也可以对尝试连接次数等其它很多内容进行限制)。方式就是可...

bisal
2013/10/16
0
0
如何解锁Oracle数据库中账号

在创建数据库时,其中SYS登录Oracle数据库在SQL*Plus工具通过如下SQL解锁。 1、通过数据字典dba_users,查看Oracle账户的锁定状态,如下:(v其中,OPEN表示账户为解锁状态;EXPIRED表示账户...

xiaocao13140
2017/10/19
0
0
freeswitch lua api使用

Lua API使用 引用链接:https://wiki.freeswitch.org/wiki/Mod_lua 10 API 10.1 Events 10.1.1 event:addBody 10.1.2 event:addHeader 10.1.3 event:delHeader 10.1.4 event:fire 10.1.5 eve......

zhouhl
2016/01/05
1
0

没有更多内容

加载失败,请刷新页面

加载更多

cxf框架的介绍

小小小施爷
9分钟前
1
0
35K成功入职:蚂蚁金服面试Java后端经历!

上个月4号通过阿里工作的学长进行内推,7天简历评估,11号接到电话面试,尽管猝不及防回答仓促,但好在前期准备充分,通过。3天后进行现场面试,通知时间为早上10点。当日设了七点闹钟,结果...

别打我会飞
9分钟前
0
0
【HAVENT原创】让 IE6 ~ IE8 浏览器也支持 map 和 filter 方法

Array.prototype 扩展可以让 IE6 ~ IE8 浏览器也支持 map 的方法: if (typeof Array.prototype.map != "function") { Array.prototype.map = function (fn, context) { var arr = [......

HAVENT
10分钟前
0
0
SMSSDK的Unity3D的两种集成方式

SMSSDK的Unity3D插件主要为用户提供了两种集成的方式,一种是通过桥接文件直接调用SMSSDK的原生API,另外一种是集成SMSSDK_Demo中的UI,这两种方式的集成,方便用户根据自己的需要进行不同的...

佳妮
18分钟前
0
0
云计算、大数据、编程语言学习指南下载,100+技术课程免费学!这份诚意满满的新年技术大礼包,你Get了吗?

开发者认证、云学院、技术社群,更多精彩,尽在开发者会场 近年来,新技术发展迅速。互联网行业持续高速增长,平均薪资水平持续提升,互联网技术学习已俨然成为学生、在职人员都感兴趣的“业...

zhaowei121
21分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部