图片服务器  带宽越来越不够用,还有当一台服务器的机房出问题的时候,不影响 整个web,以及 考虑网通电信访问服务器的 速度,所以考虑使用多台 图片 服务器

这个时候要求 图片服务器 内容是同步 的 
所以写了此程序,写的比较烂,还请批评指正, 
也好让我有所提高 
我在测试的时候通过,修改 system32/dirvers/etc/HOST 来实现 test.com 域名

web.config 中的内容如下:

<?xml version="1.0"?>
<configuration>
  <appSettings>    
    <add key="upload1" value="http://1.test.com:81/ReqFile.aspx" />  <!--这里是第一台图片服务器-->
    <add key="upload2" value="http://2.test.com:81/ReqFile.aspx" />  <!--这里是第二台图片服务器-->
    <add key="upload3" value="http://3.test.com:88/upload.php" />    <!--这里是第三台图片服务器-->
    <add key="imgurlprev" value="http://images.test.com:81/files/" />  <!--这个是上传后图片 生成图片的 url 前缀-->
    <add key="imgserverpwd" value="930B194D9C47126CFFE430720CCADBB4" /> <!--这里在 url 中加入密文,用于解密 -->
  </appSettings>
  
  <connectionStrings/>
  <system.web>
    <pages enableEventValidation="false" viewStateEncryptionMode="Never" />
    <compilation debug="true"/>
    <authentication mode="Windows" />
  </system.web>
</configuration>

​1. [代码]本地上传

/*本地上传*/
/*
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="upload_index" %>
<form method="post" runat="server" action="?action=save" enctype="multipart/form-data">
<input runat="server" type="file" />
<input type="submit" value=" 上  传 " />
</form>
*/
 
 
using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Web;
 
 
public partial class upload_index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString["action"];
        if (action == "save")
        {
            Response.ContentType = "text/html;charset=utf-8";
 
            Response.Write("<style type=\"text/css\">p{ font-size:12px; line-height:16px; margin:0; padding:0}</style>");
            HttpFileCollection HFC = Request.Files;
 
            for (int i = 0; i < HFC.Count; i++)
            {
                HttpPostedFile currentFile = HFC[i];
                TransportFile(currentFile);
            }  
        }
    }
 
 
    private void TransportFile(HttpPostedFile File)
    {
        Stream s = File.InputStream;
        byte[] byts = new byte[s.Length];
        s.Read(byts, 0, byts.Length);
        s.Close();
        s.Dispose();
 
        if (File.FileName.LastIndexOf(".") >= 0)
        {
            Random ra = new Random();
            string nowFileName = DateTime.Now.Subtract(new DateTime(2000, 1, 1)).TotalMilliseconds.ToString().Replace(".", "") + ra.Next() + File.FileName.Substring(File.FileName.LastIndexOf("."));
 
            NameValueCollection NVC = System.Configuration.ConfigurationManager.AppSettings;
 
            for (int i = 0; i < NVC.Count; i++)
            {http://www.huiyi8.com/yanjiangzhici/​
                if (NVC.Keys[i].IndexOf("upload") == 0)
                {演讲致辞
                    PostFile(NVC[i], byts, nowFileName);
                }
            }
 
            Response.Write("<p style=\"line-height:36px; display:block; height:36px;\">图片地址:<a href=\"" + System.Configuration.ConfigurationManager.AppSettings["imgurlprev"].ToString() + nowFileName + "\" target=\"_blank\">" + System.Configuration.ConfigurationManager.AppSettings["imgurlprev"].ToString() + nowFileName + "</a></p>");
        }
 
 
 
    }
 
    private void PostFile(string url,byte[] data,string fileName)
    {
 
        string pwd = System.Configuration.ConfigurationManager.AppSettings["imgserverpwd"].ToString();
        HttpWebRequest HRQ = (HttpWebRequest)System.Net.WebRequest.Create(url + "?filename=" + fileName + "&p=" + pwd);        
        HRQ.Method = "POST";
        HRQ.KeepAlive = false;
        HRQ.ContentType = "multipart/form-data";
        HRQ.Timeout = 10 * 1000;
        HRQ.ContentLength = data.Length;
        Stream sr = HRQ.GetRequestStream();
        sr.Write(data, 0, data.Length);
        HttpWebResponse RES = (HttpWebResponse)HRQ.GetResponse();
         
   
        if (HRQ.HaveResponse)
        {
            Stream Rs = RES.GetResponseStream();                      
            StreamReader RsRead = new StreamReader(Rs);
            Response.Write(RsRead.ReadToEnd());
        }
        else
        {
            Response.Write("<p>" + url + ":<span style=\"color:#f00\">失败</span></p>");            
        }
        sr.Close();
        sr.Dispose();
    }
}
2. [代码]接收端
<%@ Page Language="C#" AutoEventWireup="true" %>
<%
    string p = Request.QueryString["p"];
    string pwd = System.Configuration.ConfigurationManager.AppSettings["imgserverpwd"].ToString();
    if (p == pwd)
    {
        string FolderPath = Server.MapPath("/files");
        string filename = Request.QueryString["filename"];
 
        System.IO.Stream stream = Request.InputStream;
        byte[] buffer = new byte[stream.Length];
        stream.Read(buffer, 0, (int)stream.Length);
        Random ra = new Random();
 
 
        string nowFilePath = FolderPath + "/" + filename;
        System.IO.File.WriteAllBytes(nowFilePath, buffer);
        Response.Write("<p>" + HttpContext.Current.Request.Url.Host + " " + filename + ":<span style=\"color:#999\">上传成功</span></p>");
    }
 
     
%>

最新文章

  1. 文本处理三剑客之sed命令
  2. Centos下MySQL主从同步配置
  3. AliOS编译安装MyRocks
  4. BASE64编码和解码(VC源代码) 并 内存加载 CImage 图像
  5. cf319.B. Modulo Sum(dp &amp;&amp; 鸽巢原理 &amp;&amp; 同余模)
  6. 导入导出Excel
  7. Sql Server CTE递归
  8. [C#]AccessUtils
  9. [信息OJ 2467] Asakura的难题
  10. Unix/Linux环境C编程入门教程(11) 开发环境搭建VMWare虚拟安装之虚拟化检测
  11. ZIOZIA_百度百科
  12. jquery.validate 验证(支持前台js验证通过,然后ajax后台数据校验)二
  13. Solr集群搭建
  14. linux下如何解压和压缩文件
  15. 荣耀10带来AI版WPS,玩转潮酷新功能
  16. 类ArrayList
  17. Web API之基于H5客户端分段上传大文件
  18. python中一些传参事情
  19. Elasticsearch&#160;Query&#160;DSL
  20. 入门项目 A2 setting

热门文章

  1. 机器学习13—PCA学习笔记
  2. 在Windows 10中开启开发者模式
  3. CentOS 源码安装svn
  4. ThinkPHP3.1URL分发BUG修正
  5. MIC中的数据传输
  6. JSP指令用来设置整个JSP页面相关的属性
  7. [T-SQL] 获取拼音
  8. php在web端播放amr语音(如微信语音)
  9. Django 之 ModelForm 组件
  10. 默写一个socket客户端和socket服务端的基本通信,即:收发消息