您的位置:首页 > 其它

服务器与客户端之间的远程图片传输

2008-10-21 16:42 375 查看
一、共享类

定义一个远程对象类的定义:

接口:

public interface IFileServer
{
byte[] DownLoad(string path, string user, bool rplace);//实现从服务器上下载图片的方法
void UpLoad(string path, string user, bool rplace, byte[] bytes););//实现从客户端上传图片的方法
}

实现的方法:

public class FileClass : MarshalByRefObject, IFileServer
{
#region IFileServer 成员

public byte[] DownLoad(string path, string user, bool rplace)
{
string filepath = path+user+".jpg";
using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
{
byte[] bt = new byte[fs.Length];
fs.Read(bt, 0,(int) fs.Length);//将文件写为字节数组
fs.Close();
return bt;
}
}

public void UpLoad(string path, string user, bool rplace, byte[] bytes)
{
string filepath = path + user + ".jpg";
using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate,FileAccess.Write))
{
fs.Write(bytes , 0, (int)bytes.Length);
fs.Close();
}
}

#endregion
}

二、服务器端

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp ;
using File;
using System.Security.Permissions;
using System.Runtime.Remoting;
namespace FileServer
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
TcpServerChannel HPChannel = new TcpServerChannel(9002);
ChannelServices.RegisterChannel(HPChannel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(FileClass), "fileserver",
WellKnownObjectMode.Singleton);
MessageBox.Show("服务已经启动!");
}
}
}

三、客户端程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using File;
using System.IO;

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

private void button1_Click(object sender, EventArgs e)
{
TcpClientChannel hc = new TcpClientChannel();
ChannelServices.RegisterChannel(hc);
IFileServer IFileS = (IFileServer)Activator.GetObject(typeof(IFileServer), "tcp://" + textBox2.Text + ":9002/fileserver");
string user = textBox1.Text;
byte[] bytes;

bytes = IFileS.DownLoad(@"D:/", user, false);

using (MemoryStream ms = new MemoryStream(bytes))
{
Image bp1 = Image.FromStream(ms);
pictureBox1.Image = bp1;
ms.Close();
}
}

private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.Title = "打开图像文件";
fd.Filter = "图像文件(*.jpg)|*.JPG|位图(*.bmp)|*.bmp|所有文件(*.*)|*.*";
if (fd.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(fd.FileName);
}

}

private void button3_Click(object sender, EventArgs e)
{
IFileServer IFileS = (IFileServer)Activator.GetObject(typeof(IFileServer), "tcp://" + textBox2.Text + ":9002/fileserver");

string user = textBox1.Text;
byte[] bytes;
using (MemoryStream stream = new MemoryStream())
{
pictureBox1.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);
int length = (int)stream.Length;
bytes = stream.ToArray();
stream.Close();
}
IFileS.UpLoad(@"D:/", user, false, bytes);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐