MSMQ消息队列下文件传输例子
//远程消息发送private void remote_msg_send_Click(object sender, EventArgs e)
{
/*//远程消息发送 string格式消息
System.Messaging.Message message = new System.Messaging.Message();
message.Body = this.remote_send_msg.Text.Trim();
//message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
targetMessageQueue.Send(message);
*/
//远程消息发送 string格式文件传输
try
{
System.IO.FileStream inFile = new FileStream("C:\\zhouxingchi.mp3", System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] binaryData = new byte[inFile.Length];
inFile.Read(binaryData, 0, (int)inFile.Length);
string mStr = Convert.ToBase64String(binaryData);
string hh = mStr;
System.Messaging.Message Msg;
Msg = new System.Messaging.Message();
Msg.Label = "[mp3音乐]";
Msg.Body = mStr;
targetMessageQueue.Send(Msg);
//targetMessageQueue.Close();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
}
//远程消息接收
private void remote_msg_receive_Click(object sender, EventArgs e)
{
/*
//远程消息接收 string格式
string messageQueueAddr = "FormatName:Direct=" + this.remote_IP_address.Text + "\\Private$\\" + this.remote_mq.Text;
//System.Messaging.MessageQueue Mq;
// Mq = new System.Messaging.MessageQueue(local_mq.Text);
System.Messaging.Message Msg = this.targetMessageQueue.Receive();
Msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(String) });
//remote_receive_msg.Text = messageQueueAddr + "[" + this.targetMessageQueue.Label + ":]";
remote_receive_msg.Text = messageQueueAddr + "[" + ":]";
remote_receive_msg.Text = remote_receive_msg.Text + Msg.Body.ToString();
* */
//异地消息接收 XML消息格式
/*
targetMessageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(System.Xml.XmlDocument) });
System.Messaging.Message Msg = new System.Messaging.Message();
Msg = targetMessageQueue.Receive();
System.Xml.XmlDocument rcdata;
rcdata = new System.Xml.XmlDocument();
rcdata = (System.Xml.XmlDocument)Msg.Body;
XmlNode node = rcdata.DocumentElement.SelectSingleNode("me");
XmlElement ele = (XmlElement)node;//获取一个xml元素
string pic = ele.GetAttribute("aa");//获取ele元素的aa属性并报讯在一个临时字符串变量pic
byte[] bytes = Convert.FromBase64String(pic);//声明一个byte[]用来存放Base64解码转换过来的数据流
//创建文件流并保存
FileStream outfile = new System.IO.FileStream("C:\\zhou_xingchi.mp3", System.IO.FileMode.CreateNew);
outfile.Write(bytes, 0, (int)bytes.Length);
targetMessageQueue.Close();
* */
//异地消息接收 string格式文件传输
targetMessageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
System.Messaging.Message Msg = new System.Messaging.Message();
Msg = targetMessageQueue.Receive();
//targetMessageQueue.Close();
string pic = (string)Msg.Body;
byte[] bytes = Convert.FromBase64String(pic);//声明一个byte[]用来存放Base64解码转换过来的数据流
//创建文件流并保存
FileStream outfile = new System.IO.FileStream("C:\\zhou_xingchi.mp3", System.IO.FileMode.CreateNew);
outfile.Write(bytes, 0, (int)bytes.Length);
// Mq.Close();
} --------------------编程问答-------------------- //本地消息发送 二进制格式文件传输
try
{
this.localMessageQueue = new MessageQueue(local_mq.Text);
System.IO.FileStream inFile = new FileStream("d:\\zhouxingchi.mp3", System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] binaryData = new byte[inFile.Length];
inFile.Read(binaryData, 0, (int)inFile.Length);
string mStr = Convert.ToBase64String(binaryData);
string hh = mStr;
System.Messaging.Message Msg = new System.Messaging.Message(binaryData, new BinaryMessageFormatter());
//使用BinaryMessageFormatter这个类型进行串行化,默认情况下,这个属性不进行赋值的话就是XmlMessageFormatter
//System.Messaging.Message Msg = new System.Messaging.Message();
// Msg.Formatter = new BinaryMessageFormatter();
Msg.Label = "[mp3音乐]";
//Msg.Body = mStr;
localMessageQueue.Send(Msg);
localMessageQueue.Close();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
//本地消息接收 二进制格式文件传输
//using System.Runtime.Serialization.Formatters.Binary;
this.localMessageQueue = new MessageQueue(local_mq.Text);
byte[] bytes=new byte[1024*1024*4];
System.Messaging.Message Msg = new System.Messaging.Message(bytes, new BinaryMessageFormatter());
//使用BinaryMessageFormatter这个类型进行串行化,默认情况下,这个属性不进行赋值的话就是XmlMessageFormatter
Msg = localMessageQueue.Receive();
IFormatter bf = new BinaryFormatter();
object obj = bf.Deserialize(Msg.BodyStream);
bytes= (byte[])obj;
//bytes= (byte[])Msg.Body;
//byte[] bytes = Convert.FromBase64String(pic);//声明一个byte[]用来存放Base64解码转换过来的数据流
//byte[] bytes = (byte[])Msg.Body;
//声明一个byte[]用来存放数据流
//创建文件流并保存
FileStream outfile = new System.IO.FileStream("C:\\zhou_xingchi.mp3", System.IO.FileMode.CreateNew);
outfile.Write(bytes, 0, (int)bytes.Length);
localMessageQueue.Close();
补充:.NET技术 , C#