C# 通过WebService方式 IIS发布网站 上传文件到服务器的虚拟机下 详尽方法

应用场景:要将本地的文件 上传到 服务器的虚拟机上

网络环境:公司局域网(如下图中 第二种)

开发环境:VS2010  

服务器环境:WinServer2008    虚拟机环境:WinServer2008

 

我的程序结构目录

AppSrvice 是服务文件 将来发布了以后要放到服务器上, WindowFormsAppp 是Winform程序

 

第一步:创建一个新的: Windows窗体应用程序

关键代码如下:

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Security.Cryptography;  
  11. using System.IO;  
  12.   
  13. namespace WindowsFormsApplication1  
  14. {  
  15.     public partial class Form1 : Form  
  16.     {  
  17.         public Form1()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.   
  22.         private void button1_Click(object sender, EventArgs e)  
  23.         {  
  24.             //localhost.WebService1 client = new localhost.WebService1();  
  25.             ServiceReference1.WebService1SoapClient client = new ServiceReference1.WebService1SoapClient();  
  26.             //上传服务器后的文件名  一般不修改文件名称  
  27.             int start = textBox1.Text.LastIndexOf("\\");  
  28.             int length = textBox1.Text.Length;  
  29.             string serverfile = textBox1.Text.Substring(start + 1, length - textBox1.Text.LastIndexOf("."))  
  30.                     + DateTime.Now.ToString("-yyyy-mm-dd-hh-mm-ss")  
  31.                     + textBox1.Text.Substring(textBox1.Text.LastIndexOf("."), textBox1.Text.Length - textBox1.Text.LastIndexOf("."));  
  32.   
  33.             client.CreateFile(serverfile);  
  34.             //要上传文件的路径  
  35.             string sourceFile = textBox1.Text ;  
  36.             string md5 = GetMD5(sourceFile);  
  37.   
  38.             FileStream fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);  
  39.             int size = (int)fs.Length;  
  40.             int bufferSize = 1024 * 512;  
  41.             int count = (int)Math.Ceiling((double)size / (double)bufferSize);  
  42.             for (int i = 0; i < count; i++)  
  43.             {  
  44.                 int readSize = bufferSize;  
  45.                 if (i == count - 1)  
  46.                     readSize = size - bufferSize * i;  
  47.                 byte[] buffer = new byte[readSize];  
  48.                 fs.Read(buffer, 0, readSize);  
  49.                 client.Append(serverfile, buffer);  
  50.             }  
  51.   
  52.             bool isVerify = client.Verify(serverfile, md5);  
  53.             if (isVerify)  
  54.                 MessageBox.Show("上传成功");  
  55.             else  
  56.                 MessageBox.Show("上传失败");  
  57.   
  58.         }  
  59.   
  60.         private string GetMD5(string fileName)  
  61.         {  
  62.             FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);  
  63.             MD5CryptoServiceProvider p = new MD5CryptoServiceProvider();  
  64.             byte[] md5buffer = p.ComputeHash(fs);  
  65.             fs.Close();  
  66.             string md5Str = "";  
  67.             List<string> strList = new List<string>();  
  68.             for (int i = 0; i < md5buffer.Length; i++)  
  69.             {  
  70.                 md5Str += md5buffer[i].ToString("x2");  
  71.             }  
  72.             return md5Str;  
  73.         }  
  74.   
  75.         private void button2_Click(object sender, EventArgs e)  
  76.         {  
  77.             OpenFileDialog openDialog = new OpenFileDialog();  
  78.             openDialog.Filter = "视频文件(*.avi,*.wmv,*.mp4)|*.avi;*.wmv;*.mp4";  
  79.             if (openDialog.ShowDialog() == DialogResult.OK)  
  80.             {  
  81.                 textBox1.Text = openDialog.FileName;  
  82.             }  
  83.         }  
  84.   
  85.   
  86.     }  
  87. }  


 


第二步:创建WebService

关键代码如下:

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6. using System.IO;  
  7. using System.Security.Cryptography;  
  8. namespace StateGrid95598  
  9. {  
  10.     /// <summary>  
  11.     /// WebService1 的摘要说明  
  12.     /// </summary>  
  13.     [WebService(Namespace = "http://tempuri.org/")]  
  14.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  15.     [System.ComponentModel.ToolboxItem(false)]  
  16.     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。   
  17.     // [System.Web.Script.Services.ScriptService]  
  18.     public class WebService1 : System.Web.Services.WebService  
  19.     {  
  20.   
  21.         [WebMethod]  
  22.         public string HelloWorld()  
  23.         {  
  24.             return "Hello World";  
  25.         }  
  26.   
  27.         [WebMethod]  
  28.         public bool CreateFile(string fileName)  
  29.         {  
  30.             bool isCreate = true;  
  31.             try  
  32.             {  
  33.                 //首先设置上传服务器文件的路径  然后发布web服务 发布的时候要自己建一个自己知道的文件夹 "C:\NMGIS_Video\" "C:\NMGIS_Video\"                fileName = Path.Combine(Server.MapPath("") + @"\Video\" + Path.GetFileName(fileName));  
  34.                 FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);  
  35.                 fs.Close();  
  36.             }  
  37.             catch  
  38.             {  
  39.                 isCreate = false;  
  40.             }  
  41.             return isCreate;  
  42.         }  
  43.         [WebMethod]  
  44.         public bool Append(string fileName, byte[] buffer)  
  45.         {  
  46.             bool isAppend = true;  
  47.             try  
  48.             {  
  49.                 //fileName = Path.Combine(@"C:\NMGIS_Video\" + Path.GetFileName(fileName));  
  50.                 fileName = Path.Combine(Server.MapPath("") + @"\Video\" + Path.GetFileName(fileName));  
  51.                 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);  
  52.                 fs.Seek(0, SeekOrigin.End);  
  53.                 fs.Write(buffer, 0, buffer.Length);  
  54.                 fs.Close();  
  55.             }  
  56.             catch  
  57.             {  
  58.                 isAppend = false;  
  59.             }  
  60.             return isAppend;  
  61.         }  
  62.         [WebMethod]  
  63.         public bool Verify(string fileName, string md5)  
  64.         {  
  65.             bool isVerify = true;  
  66.             try  
  67.             {  
  68.   
  69.                 fileName = Path.Combine(Server.MapPath("") + @"\Video\" + Path.GetFileName(fileName));  
  70.                 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);  
  71.                 MD5CryptoServiceProvider p = new MD5CryptoServiceProvider();  
  72.                 byte[] md5buffer = p.ComputeHash(fs);  
  73.                 fs.Close();  
  74.                 string md5Str = "";  
  75.                 List<string> strList = new List<string>();  
  76.                 for (int i = 0; i < md5buffer.Length; i++)  
  77.                 {  
  78.                     md5Str += md5buffer[i].ToString("x2");  
  79.                 }  
  80.                 if (md5 != md5Str)  
  81.                     isVerify = false;  
  82.             }  
  83.             catch  
  84.             {  
  85.                 isVerify = false;  
  86.             }  
  87.             return isVerify;  
  88.         }  
  89.     }  
  90. }  


第三步:发布服务

选中服务项目,右键 发布

 

发布方法选择:文件系统

目标位置:是选择你发布后生成文件的位置 自己随便找个地方即可

然后点击 发布

 

第四步:拷贝文件到服务器

将刚才发布好的文件拷贝到你要上传到的服务器的虚拟机的指定目录下

 

 

第五步:在虚拟机上发布网站

打开虚拟机的IIS 发布一个网站 文件路径指向你刚才拷贝到虚拟机上的文件目录

IP地址就是当前虚拟机的IP  要设置为固定的IP

端口一定注意 不要与当前正在使用的端口冲突 建议更改一个

然后确定 发布网站

 

 

选中刚才发布的网站 ,右边滚动条向下,选择 默认文档 并双击

 

双击打开后右边点击添加按钮 ,当刚才复制到虚拟机当中的 .asmx 文件名添加到里边点确定

 

网站右键 ,管理网站,浏览  查看发不好的网站是否可以访问

 

我这里浏览是可以访问的:如下图

 

第六步:设置虚拟机网络环境

虚拟机》编辑  或者  开始菜单中 找到 Virtral Network Editor

 

打开虚拟网络编辑器

 

Nat 设置里边 映射两个端口  TCP、UDP类型各一个, 然后点击确定

宿主机的8070 端口映射到虚拟机的“192.168.16.135”的8070端口了,因为web服务自动开放的端口是8070,所以,只要我们访问 “http://192.168.1.54:8070”,就可以访问到虚拟机的8070端口,也就是web服务了 (这里宿主机的端口可以改成其他端口,无需跟虚拟机端口一致,我是为了省事都写成了8070)

 

然后点击应用 确定  。。。 这里设置好了以后就可以通过访问虚拟机的宿主机IP访问到你虚拟机上的服务

 

 此时就可以通过其他任意机器访问你的.asmx页面 注意你的端口一定要正确 正如上面所说:这里直接通过访问宿主机的Ip就可以

 

第七步:为客户端添加服务器引用

 项目右键 添加服务引用

 

添加服务地址 点击 前往 ,如果正确的话 会在下面显示出你的服务页面 然后点击确定

 

 第八步:测试

运行客户端测试

 

这里显示成功了  那么我们去虚拟机的目录下看一看到底有没有

 

这里有两个文件 我测试了两次 ,  文件名称 我在程序当中追加了当前时间的格式化字符串,文件大小也是对的。

免积分 源码下载 地址:http://download.csdn.net/detail/u010011052/7213805

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值