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

    技术2022-05-11  113

    作者:tonnyEMAIL:webmaster@weiw.com转载请显示出处: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<%@ Page Language="C#" %><HTML> <HEAD>  <title>CreateDirectory</title>  <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">  <meta name="CODE_LANGUAGE" Content="C#">  <meta name="vs_defaultClientScript" content="JavaScript">  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout">  <form id="CreateDirectory" method="post" runat="server">   <FONT face="宋体"></FONT>  </form> </body></HTML><% string path = Request.Params["Path"].ToString().Trim(); System.IO.Directory.CreateDirectory(Server.MapPath(path));%>

     

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

    /// <summary>/// 在Web目录基础下创建文本文件/// </summary>/// <param name="fileSpec">指定的文件(如WebOut/Article/1/20021022093422.html)</param>/// <param name="content">文本文件的内容</param>/// <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<%@ Page Language="C#" %><HTML> <HEAD>  <title>ReNameFile</title>  <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">  <meta name="CODE_LANGUAGE" Content="C#">  <meta name="vs_defaultClientScript" content="JavaScript">  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout">  <form id="CreateDirectory" method="post" runat="server">   <FONT face="宋体"></FONT>  </form> </body></HTML><% string path = Request.Params["Path"].ToString().Trim(); System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath(path));

     string content; System.IO.StreamReader oRead = fi.OpenText(); content = oRead.ReadToEnd(); oRead.Close(); System.IO.FileStream fileStream = new System.IO.FileStream( Server.MapPath(path), System.IO.FileMode.Create ); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter( fileStream ,System.Text.Encoding.Default ); streamWriter.Write( content ); streamWriter.Close(); if (path.IndexOf("html")>0)  {  path = path.Replace("html","aspx");  fi.MoveTo(Server.MapPath(path)); }%>

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

     


    最新回复(0)