局域网内基于WEB的文件传输解决方案详解

作者:tonny
EMAIL: [email protected]
转载请显示出处: http://www.weiw.com

环境说明:

局域网内的两台服务器,一台当作主WEB服务器,一台作文件服务器,
两服务器操作系统为win2000 professional(或win2000 server)
主WEB服务器 局域网内部URL: http://main:8080 外部URL
服务器计算机名为:main WEB站点的端口为8080

文件服务器 局域网内部URL: http://file:8080
服务器计算机名为:file WEB站点的端口为8080

需求介绍:
访问者访问WEB服务器,上传文件时将文件放于文件服务器,如生成一些信息之类的静态页面,也在文件服务器上生成。WEB服务器并不保留文件副本。并且允许生成ASPX格式的文件。

解决方法:
文件服务器IIS设置站点。 http://file:8080 ,设一目录用于存放上传文件,命名为UpFile。
并且把当前目录属性-->安全-->权限中加入一预留给专供上传的域用户账号,权限当然是完全控制。

在WEB服务器上,根目录下原先有Upfile目录。现将此目录从IIS中删除,然后在IIS中建立一虚拟目录,名为Upfile,目标指向文件服务器上的Upfile目录。当然还是要加上可写可读的权限。

将文件上传至文件服务器的程序例子:
test.cs (运行于Web服务器上的程序)
using System.Text;

string virtualPath="test01"; //欲建立在upfile目录下的文件夹名称;
string rootUpfilePath = @" http://file:8080/upfile/ "; //文件服务器 (非本程序运行的服务器)
string uriString = rootUpfilePath + virtualPath +"/"; //URL路径

//CreateDirectory.aspx 此文件用于在目的服务器(文件服务器)上Upfile目录下,用于建立相应目录。
string path = rootUpfilePath + "CreateDirectory.aspx?Path="+virtualPath;

string filename="test.htm"; //此为生成的文件名

string MyString = "这是建立的文件内容" ;
UTF8Encoding AE = new UTF8Encoding();

byte[] input = AE.GetBytes(MyString);
int intLength=input.Length;

string username = @"domain_a\cqweb"; //“域名称\域用户名”
string password = @"123456"; //域用户密码

System.Net.NetworkCredential myCred = new System.Net.NetworkCredential(username, password);
//为基于密码的身份验证方案提供凭据
System.Net.WebClient Client = new System.Net.WebClient();
//提供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法
System.Net.CredentialCache myCache = new System.Net.CredentialCache();
//存储 Internet 资源的凭据
myCache.Add(new Uri(uriString), "NTLM", myCred);
//向凭据缓存添加 NetworkCredential 实例

byte[] buffer = new byte[128];

Stream stream = Client.OpenRead(path);
stream.Read(buffer, 0, 128); // 创建目录

System.IO.Stream writeStream = Client.OpenWrite(uriString + filename ,"PUT");
writeStream.Write (input, 0, intLength);
writeStream.Close();

文件服务器 上的文件CreateDirectory.aspx

1@ Page Language="C#" 
 1<html>
 2<head>
 3<title>CreateDirectory</title>
 4<meta content="Microsoft Visual Studio 7.0" name="GENERATOR"/>
 5<meta content="C#" name="CODE_LANGUAGE"/>
 6<meta content="JavaScript" name="vs_defaultClientScript"/>
 7<meta content=" http://schemas.microsoft.com/intellisense/ie5 " name="vs_targetSchema"/>
 8</head>
 9<body ms_positioning="GridLayout">
10<form id="CreateDirectory" method="post" runat="server">
11<font face="宋体"></font>
12</form>
13</body>
14</html>
1 string path = Request.Params["Path"].ToString().Trim();   
2System.IO.Directory.CreateDirectory(Server.MapPath(path));   

二、将aspx文件生成至文件服务器的程序例子:
思路:直接通过http方式传输,文件服务器端会提示禁止访问。因为安全问题,服务器对ASPX,asp等文件的文件传输有限制。限制是在.net Framework中做的,相关文件是, machine.config。如果修改此文件即可解决问题。我曾就此问题咨询过微软件技术支持,回复是,修改后会带来其他安全问题,所以最好还不改这个。暂且也就只好用改扩展名的方式。传之前,将扩展名改为html,到触发文件服务器端程序,将扩展名改回aspx。由于在处理时,文件编码为强制改为UTF-8.会出现中文乱码。所以还要把编码改为ANSI。
///

1<summary>   
2/// 在Web目录基础下创建文件夹   
3/// </summary>

///

1<param name="path"/>

目录路径,如图片库为UpFiles/PhotoLib/
///

1<returns>返回是否成功新建目录</returns>

public static bool CreateNewFolderBaseWeb(string path)
{
string rootUpfilePath = Com.Common.SzcmConfiguration.UploadFileWeb; //@"http://file:8012/ ";
string uriString = rootUpfilePath + path;
path = rootUpfilePath + "CreateDirectory.aspx?Path=" + path;
string username = Com.Common.SzcmConfiguration.UploadFileUser; //@"domain_a\cqweb ";
string password = Com.Common.SzcmConfiguration.UploadFilePassword; //@"123456 ";
System.Net.NetworkCredential myCred = new System.Net.NetworkCredential(username, password);
System.Net.WebClient Client = new System.Net.WebClient();
System.Net.CredentialCache myCache = new System.Net.CredentialCache();
// myCache.Add(new Uri(this.Page.Application["FSPATH"].ToString()),"NTLM",myCred);
myCache.Add(new Uri(uriString), "NTLM", myCred);
// Client.Credentials = myCache;
// 创建目录
//byte[] buffer = new byte[128];
Stream stream = Client.OpenRead(path);
//stream.Read(buffer, 0, 128);
}

///

1<summary>   
2/// 在Web目录基础下创建文本文件   
3/// </summary>

///

1<param name="fileSpec"/>

指定的文件(如WebOut/Article/1/20021022093422.html)
///

1<param name="content"/>

文本文件的内容
///

1<returns>返回是否成功创建文件</returns>

public static bool CreateTxtFileBaseWeb(string fileSpec, string content)
{
string path;
try
{
fileSpec = fileSpec.Replace("aspx","html"); //先把扩展名由aspx改为html
string virtualPath = fileSpec.Substring(0,fileSpec.LastIndexOf("/"));
string rootUpfilePath = Com.Common.SzcmConfiguration.UploadFileWeb; //@"http://file:8012/ ";
string uriString = rootUpfilePath + virtualPath + "/"; //"upfiles/";
//在文件服务器上把生成的html扩展名改为aspx
path = rootUpfilePath + "ReNameFile.aspx?Path="+fileSpec;

System.Text.UTF8Encoding AE = new System.Text.UTF8Encoding();
byte[] input = AE.GetBytes(content);
//byte[] input = System.Text.Encoding.GetEncoding("UTF8").GetBytes(content);
int intLength=input.Length;
string username = Com.Common.SzcmConfiguration.UploadFileUser; //@"domain_a\cqweb ";
string password = Com.Common.SzcmConfiguration.UploadFilePassword; //@"123456 ";
System.Net.NetworkCredential myCred = new System.Net.NetworkCredential(username, password);
System.Net.WebClient Client = new System.Net.WebClient();

System.IO.Stream writeStream = Client.OpenWrite(rootUpfilePath + fileSpec ,"PUT");

//System.IO.Stream writeStream = Client.OpenWrite(rootUpfilePath + "upfiles/tt.txt" ,"PUT");
writeStream.Write(input, 0, intLength);
writeStream.Close();
Stream stream = Client.OpenRead(path); //在触发文件服务器端的改名程序。
return true;
}
catch (Exception ex )
{
throw ex;
return false;
}
}

文件服务器 上的文件ReNameFile.aspx

1@ Page Language="C#" 
 1<html>
 2<head>
 3<title>ReNameFile</title>
 4<meta content="Microsoft Visual Studio 7.0" name="GENERATOR"/>
 5<meta content="C#" name="CODE_LANGUAGE"/>
 6<meta content="JavaScript" name="vs_defaultClientScript"/>
 7<meta content=" http://schemas.microsoft.com/intellisense/ie5 " name="vs_targetSchema"/>
 8</head>
 9<body ms_positioning="GridLayout">
10<form id="CreateDirectory" method="post" runat="server">
11<font face="宋体"></font>
12</form>
13</body>
14</html>
 1 string path = Request.Params["Path"].ToString().Trim();   
 2System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath(path)); 
 3
 4string content;   
 5System.IO.StreamReader oRead = fi.OpenText();   
 6content = oRead.ReadToEnd();   
 7oRead.Close();   
 8System.IO.FileStream fileStream = new System.IO.FileStream( Server.MapPath(path), System.IO.FileMode.Create );   
 9System.IO.StreamWriter streamWriter = new System.IO.StreamWriter( fileStream ,System.Text.Encoding.Default );   
10streamWriter.Write( content );   
11streamWriter.Close();   
12if (path.IndexOf("html")>0)   
13{   
14path = path.Replace("html","aspx");   
15fi.MoveTo(Server.MapPath(path));   
16}   

注意:此时,如果仍出现错误,提示无权限,可能是WEB服务器的存放临时文件处的权限问题。还要设置一下Framework.
C:\WINNT\Microsoft.NET\Framework\v1.0.3705\Temporary ASP.NET Files
把此目录权限开放给everyone
再检查一下machine.config中配置节名为processModel的userName属性值是否为"本机名\aspnet"

Published At
Categories with Web编程
Tagged with
comments powered by Disqus