您的位置:首页 > 运维架构 > 网站架构

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

2014-04-14 20:38 971 查看

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

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



开发环境:VS2010  

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

 

我的程序结构目录

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



 

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



关键代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//localhost.WebService1 client = new localhost.WebService1();
ServiceReference1.WebService1SoapClient client = new ServiceReference1.WebService1SoapClient();
//上传服务器后的文件名  一般不修改文件名称
int start = textBox1.Text.LastIndexOf("\\");
int length = textBox1.Text.Length;
string serverfile = textBox1.Text.Substring(start + 1, length - textBox1.Text.LastIndexOf("."))
+ DateTime.Now.ToString("-yyyy-mm-dd-hh-mm-ss")
+ textBox1.Text.Substring(textBox1.Text.LastIndexOf("."), textBox1.Text.Length - textBox1.Text.LastIndexOf("."));

client.CreateFile(serverfile);
//要上传文件的路径
string sourceFile = textBox1.Text ;
string md5 = GetMD5(sourceFile);

FileStream fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
int size = (int)fs.Length;
int bufferSize = 1024 * 512;
int count = (int)Math.Ceiling((double)size / (double)bufferSize);
for (int i = 0; i < count; i++)
{
int readSize = bufferSize;
if (i == count - 1)
readSize = size - bufferSize * i;
byte[] buffer = new byte[readSize];
fs.Read(buffer, 0, readSize);
client.Append(serverfile, buffer);
}

bool isVerify = client.Verify(serverfile, md5);
if (isVerify)
MessageBox.Show("上传成功");
else
MessageBox.Show("上传失败");

}

private string GetMD5(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
MD5CryptoServiceProvider p = new MD5CryptoServiceProvider();
byte[] md5buffer = p.ComputeHash(fs);
fs.Close();
string md5Str = "";
List<string> strList = new List<string>();
for (int i = 0; i < md5buffer.Length; i++)
{
md5Str += md5buffer[i].ToString("x2");
}
return md5Str;
}

private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Filter = "视频文件(*.avi,*.wmv,*.mp4)|*.avi;*.wmv;*.mp4";
if (openDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openDialog.FileName;
}
}

}
}


 



第二步:创建WebService

关键代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Security.Cryptography;
namespace StateGrid95598
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}

[WebMethod]
public bool CreateFile(string fileName)
{
bool isCreate = true;
try
{
//首先设置上传服务器文件的路径  然后发布web服务 发布的时候要自己建一个自己知道的文件夹 "C:\NMGIS_Video\" "C:\NMGIS_Video\"                fileName = Path.Combine(Server.MapPath("") + @"\Video\" + Path.GetFileName(fileName));
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
fs.Close();
}
catch
{
isCreate = false;
}
return isCreate;
}
[WebMethod]
public bool Append(string fileName, byte[] buffer)
{
bool isAppend = true;
try
{
//fileName = Path.Combine(@"C:\NMGIS_Video\" + Path.GetFileName(fileName));
fileName = Path.Combine(Server.MapPath("") + @"\Video\" + Path.GetFileName(fileName));
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
fs.Seek(0, SeekOrigin.End);
fs.Write(buffer, 0, buffer.Length);
fs.Close();
}
catch
{
isAppend = false;
}
return isAppend;
}
[WebMethod]
public bool Verify(string fileName, string md5)
{
bool isVerify = true;
try
{

fileName = Path.Combine(Server.MapPath("") + @"\Video\" + Path.GetFileName(fileName));
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
MD5CryptoServiceProvider p = new MD5CryptoServiceProvider();
byte[] md5buffer = p.ComputeHash(fs);
fs.Close();
string md5Str = "";
List<string> strList = new List<string>();
for (int i = 0; i < md5buffer.Length; i++)
{
md5Str += md5buffer[i].ToString("x2");
}
if (md5 != md5Str)
isVerify = false;
}
catch
{
isVerify = false;
}
return isVerify;
}
}
}



第三步:发布服务

选中服务项目,右键 发布



 

发布方法选择:文件系统

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

然后点击 发布



 

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

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



 



 

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

打开虚拟机的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

 

有问题的可以在评论中提出,我会尽快回复。个人实践,纯手写,喜欢可以点个赞,不喜勿喷!

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息