上篇說到在本機建立一個WebServices,用控制檯應用程序調用WebServices的SayHello方法。html
http://www.cnblogs.com/xw-yanger/p/3434297.html數組
今天說說把WebServices放到服務器(本機做爲服務器)上,讓局域網其餘機子也能訪問到。安全
跟上期同樣,此次寫了一個上傳圖片到服務器的WebServices服務器
代碼也很簡單,一個上傳,一個下載:工具
//上傳網站
[WebMethod]htm
public bool Up(byte[] data, string filename)blog
{事件
try圖片
{
FileStream fs = File.Create("E:\\" + filename);
fs.Write(data, 0, data.Length);
fs.Close();
return true;
}
catch
{
return false;
}
}
//下載
[WebMethod]
public byte[] Down(string filename)
{
string filepath = Server.MapPath("E:\\") + filename;
if (File.Exists(filepath))
{
try
{
FileStream s = File.OpenRead(filepath);
return ConvertStreamToByteBuffer(s);
}
catch
{
return new byte[0];
}
}
else
{
return new byte[0];
}
}
寫好以後,發佈(詳見上篇文章)
兩button,一個textBox,一個openFileDialog1
Open單擊事件代碼:
string filter = "圖片文件(*.jpg,*.gif,*.bmp)|*.jpg;*.gif;*.bmp";
openFileDialog1.Filter = filter;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
//pictureBox1.Image = Image.FromFile(textBox1.Text);
}
UpLoad單擊事件代碼:
//保存到遠程服務器
FileStream s = new FileStream(textBox1.Text, FileMode.OpenOrCreate);
UpDownFile.Service1 client = new UpDownFile.Service1();
client.Up(ConvertStreamToByteBuffer(s),"test.jpg");
將Stream流轉換爲byte數組的方法
//將Stream流轉換爲byte數組的方法
public byte[] ConvertStreamToByteBuffer(Stream s)
{
MemoryStream ms = new MemoryStream();
int b;
while ((b = s.ReadByte()) != -1)
{
ms.WriteByte((byte)b);
}
return ms.ToArray();
}
OK,代碼寫好以後,再配置網站,讓WebServices跑起來,這樣才能訪問,並且配置很關鍵,注意方法。
打開IIS,新建一個網站:
這裏注意:主機名配置的是本機的局域網IP(我在這糾結了很久才弄好的)。
這裏配置好以後啓動網站,如今想讓局域網能訪問,咱們還差最後一步:配置防火牆
網上有方法 http://hi.baidu.com/xyrrwcom/item/93043153b966a1978d12edee
這裏我也貼出來,這是win7的,XP的百度找找看。
一、開始---全部程序---管理工具---高級安全 Windows 防火牆。
二、在高級安全 Windows 防火牆的左邊欄;選擇「入站規則」。
三、在右邊欄選擇"新建規則「。
四、在彈出的窗口依次選擇:選中端口---下一步---選中TCP以及特定本地端口;
五、填入要開放的端口號(這裏填入80;當讓也能夠選擇開放全部端口
六、下一步---選中容許鏈接---下一步---選中全部選項---下一步---填入名稱(這裏填入IIS)。
完畢。
至此,應該就沒什麼問題了,把exe文件放到別的機子上運行試試,而後在看本機E盤下面是否是有一個test.jpg文件了。