图片服务器  带宽越来越不够用,还有当一台服务器的机房出问题的时候,不影响 整个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>");
    }
 
     
%>

C# 多服务器上传 示例的更多相关文章

  1. UEditor之实现配置简单的图片上传示例

    UEditor之实现配置简单的图片上传示例 原创 2016年06月11日 18:27:31 开心一笑 下班后,阿华到楼下小超市买毛巾,刚买完出来,就遇到同一办公楼里另一家公司的阿菲,之前与她远远的有过 ...

  2. 【转载】兼容php5,php7的cURL文件上传示例

    转载来自: http://www.huanlinna.com/2016/06/25/coding/php5-php7-upload-demo-via-curl.html https://segment ...

  3. jQuery AJAX 网页无刷新上传示例

    新年礼,提供简单.易套用的 jQuery AJAX 上传示例及代码下载.后台对文件的上传及检查,以 C#/.NET Handler 处理 (可视需要改写成 Java 或 PHP). 有时做一个网站项目 ...

  4. 测试必备技能系列4:如何用SSH向linux服务器上传下载文件

    通过ssh方式,向远程服务器上传文件 非常方便 直接看老徐之前的文章http://www.51testing.com/?uid-497177-action-viewspace-itemid-37054 ...

  5. ASP.NET跨服务器上传文件的相关解决方案

    第一种:通过FTP来上传文件 首先,在另外一台服务器上设置好FTP服务,并创建好允许上传的用户和密码,然后,在ASP.NET里就可以直接将文件上传到这台 FTP 服务器上了.代码如下: <%@ ...

  6. xshell终端向远程服务器上传文件方法

    centos-7下在本地终端里向远程服务器上传文件,在命令行中执行的软件. 安装命令如下: 在终端里输入如下命令: 会弹出如下窗口 选择你要上传的文件即可上传成功.

  7. android 向服务器上传

    采用数据流的格式向服务器上传. 代码如下: private void upload(String requestURL) {                          //参数requestU ...

  8. 向linux服务器上传下载文件方式收集

    向linux服务器上传下载文件方式收集 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法] scp就是secure copy,是用来进行远程文件拷贝的.数据传输使用 ...

  9. Spring学习---Spring中利用组件实现从FTP服务器上传/下载文件

    FtpUtil.java import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcepti ...

随机推荐

  1. [译]NeHe教程 - 你的第一个多边形

    原文: Your First Polygon 在第一节中我讲解了如何创建OpenGL窗体.本节我会讲解如何创建三角形和四边形.我们会用GL_TRIANGLES来创建三角形,用GL_GUADS创建四边形 ...

  2. codeforces 427 div.2 F. Roads in the Kingdom

    F. Roads in the Kingdom time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  3. android 蓝牙低耗能(LBE)技术介绍

    蓝牙低能耗(BLE)技术是低成本.短距离.可互操作的鲁棒性无线技术.工作在免许可的2.4GHz ISM射频频段.它从一開始就设计为超低功耗(ULP)无线技术. 它利用很多智能手段最大限度地减少功耗. ...

  4. named主从环境部署

    named主 1. bind服务安装配置 yum -y install bind*.x86_64 配置文件: /etc/named.conf /etc/named.rfc1912.zones /etc ...

  5. Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '' for column 'createtime' at row 1...

    之前项目一直好好的,之后电脑重装系统,数据库重新安装了一个5.6版本的,项目jar包丢失了,之后就又重新找了一些jar包倒入,结果运行报错: Caused by: com.mysql.jdbc.Mys ...

  6. xml schema复杂类型

    xml schema复杂类型 对于复杂类型,xs:complexType, xs:sequence子节点必须有. <?xml version="1.0"?> <x ...

  7. 图像处理之基础---傅立叶c实现

    http://blog.csdn.net/lzhq28/article/details/7847047 http://blog.csdn.net/lishizelibin/article/detail ...

  8. NSIS 变量

    $PROGRAMFILES 程序文件目录(通常为 C:\Program Files 但是运行时会检测). $COMMONFILES 公用文件目录.这是应用程序共享组件的目录(通常为 C:\Progra ...

  9. python 文字识别 之 pytesseract

    pytesseract资源 链接:https://pan.baidu.com/s/1eTsqhsY 密码:j0yo 安装时前面一直next就可以了,直到这一步,勾选Math和Chinese,支持计算和 ...

  10. 解决ubuntu无法进入unity模式

    终端输入如下命令: 1.sudo add-apt-repository ppa:gnome3-team/gnome3 2.sudo apt-get update 3.sudo apt-get inst ...