应用场景:要将本地的文件 上传到服务器的虚拟机上
网络环境:公司局域网(如下图中第二种)
开发环境: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;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
using System.Web.Services;
namespace WindowsFormsApplication1
{
public
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//localhost.WebService1 client = new
localhost.WebService1();
WindowsFormsApp.ServiceReference1.Service1SoapClient client = new
WindowsFormsApp.ServiceReference1.Service1SoapClient();
//WindowsFormsApp.ServiceReference1.WebService1SoapClient client =
new
WindowsFormsApp.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 strList = new List();
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;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Collections.Generic;
namespace WebService1
{
///
/// Service1
的摘要说明
///
[WebService(Namespace = "http:///")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用
ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
//
[System.Web.Script.Services.ScriptService]
public class
Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public bool CreateFile(string fileName)
{
bool isCreate = true;
try
{
fileName = Path.Combine(Server.MapPath(""),
Path.GetFileName(fileName));
//首先设置上传服务器文件的路径 然后发布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;
|