MQ 文件传输

本文介绍了通过MQ进行文件传输的步骤,包括使用序列化类FileBean存储文件名和Base64编码后的文件内容,通过MQ发送和接收消息,以及接收方如何解码并写回文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  对于MQ传输文件,我采取的思路是:
     A.先定义一个序列化类(赞命名为fileBean),类的属性有fileName和fileContent 两个。
     B.用输入流配合 BASE64Encoder 将文件格式化为 基于BASE64Encoder 的String编码 作为文件的内容。
     C.将文件名和文件内容set到fileBean的fileContent属性中。
     D.调用MQ将这个Object写到远程队列中去。
     E.接收方接受到消息时用readObject()方法读出,强转成fileBean
    F.从fileBean中取出文件名和文件内容,将文件内容用BASE64Decoder解码
     G.用文件输出流将文件写到指定的位置,到此大功告成。

-------------------------废话少说,看代码-------------------------------
  文件序列化类

  1. package com.test.mq;  
  2.   
  3. import java.io.Serializable;  
  4. /** 
  5.  *  
  6.  * <p> 
  7.  * Title: FileBean.java 
  8.  * </p> 
  9.  * <p> 
  10.  * Description: 
  11.  * </p> 
  12.  * <p> 
  13.  * Copyright: Copyright (c) 2009 
  14.  * </p> 
  15.  * <p> 
  16.  * Company: shunde 
  17.  * </p> 
  18.  *  
  19.  * @author: listening 
  20.  * @create date Nov 8, 2009 
  21.  */  
  22. public class FileBean implements Serializable {  
  23.     /** 
  24.      *  
  25.      */  
  26.     private static final long serialVersionUID = 1L;  
  27.   
  28.     private String fileName = "";// 文件名  
  29.   
  30.     private String fileContent = "";// 文件内容(BASE64Encoder编码之后的)  
  31.   
  32.     public String getFileName() {  
  33.         return fileName;  
  34.     }  
  35.   
  36.     public void setFileName(String fileName) {  
  37.         this.fileName = fileName;  
  38.     }  
  39.   
  40.     public String getFileContent() {  
  41.         return fileContent;  
  42.     }  
  43.   
  44.     public void setFileContent(String fileContent) {  
  45.         this.fileContent = fileContent;  
  46.     }  
  47.   
  48. }  
package com.test.mq;

import java.io.Serializable;
/**
 * 
 * <p>
 * Title: FileBean.java
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: shunde
 * </p>
 * 
 * @author: listening
 * @create date Nov 8, 2009
 */
public class FileBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String fileName = "";// 文件名

	private String fileContent = "";// 文件内容(BASE64Encoder编码之后的)

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileContent() {
		return fileContent;
	}

	public void setFileContent(String fileContent) {
		this.fileContent = fileContent;
	}

}


文件发送和接受类

  1. package com.test.mq;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.text.SimpleDateFormat;  
  8. import java.util.Date;  
  9.   
  10. import sun.misc.BASE64Decoder;  
  11. import sun.misc.BASE64Encoder;  
  12.   
  13. import com.ibm.mq.MQC;  
  14. import com.ibm.mq.MQEnvironment;  
  15. import com.ibm.mq.MQException;  
  16. import com.ibm.mq.MQGetMessageOptions;  
  17. import com.ibm.mq.MQMessage;  
  18. import com.ibm.mq.MQPutMessageOptions;  
  19. import com.ibm.mq.MQQueue;  
  20. import com.ibm.mq.MQQueueManager;  
  21.   
  22. /** 
  23.  *  
  24.  * <p> 
  25.  * Title: MQSendAndReceiveUtil.java 
  26.  * </p> 
  27.  * <p> 
  28.  * Description: 
  29.  * </p> 
  30.  * <p> 
  31.  * Copyright: Copyright (c) 2009 
  32.  * </p> 
  33.  * <p> 
  34.  * Company: shunde 
  35.  * </p> 
  36.  *  
  37.  * @author: listening 
  38.  * @create date Nov 8, 2009 
  39.  */  
  40. public class MQSendAndReceiveUtil {  
  41.     private MQQueueManager qManager;  
  42.     private MQQueue queue;  
  43.   
  44.     private static String qmManager = "QM_00000000";// 队列管理器名称  
  45.     private static String remoteQName = "RQ_88888888";// 远程队列名称  
  46.     private static String localQName = "LQ_00000000";// 本地队列  
  47.     private static String hostname = "192.168.1.66";// 本机名称  
  48.     private static String channel = "DC.SVRCONN";// 服务器链接通道  
  49.     private static int ccsid = 1381;  
  50.     private static int port = 1414;  
  51.   
  52.     @SuppressWarnings("unchecked")  
  53.     private MQSendAndReceiveUtil() {  
  54.         MQEnvironment.hostname = hostname;  
  55.         MQEnvironment.channel = channel;  
  56.         MQEnvironment.CCSID = ccsid;  
  57.         MQEnvironment.port = port;  
  58.         MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,  
  59.                 MQC.TRANSPORT_MQSERIES);  
  60.   
  61.         try {  
  62.             qManager = new MQQueueManager(qmManager);  
  63.         } catch (MQException e) {  
  64.             e.printStackTrace();  
  65.         }  
  66.     }  
  67.   
  68.     /** 
  69.      *  
  70.      * Description:如果队列管理器为空,建立 
  71.      *  
  72.      * @param: 
  73.      * @return: void 
  74.      * @exception Exception. 
  75.      * @author listening created at Nov 8, 2009 
  76.      */  
  77.     private void createConnection() {  
  78.         if (qManager == null) {  
  79.             new MQSendAndReceiveUtil();  
  80.         }  
  81.     }  
  82.   
  83.       
  84.     /** 
  85.      *  
  86.      * Description:发送文件 
  87.      *  
  88.      * @param:String fileName -文件名 
  89.      * @return: void 
  90.      * @exception Exception. 
  91.      * @author listening created at Nov 8, 2009 
  92.      */  
  93.     public void sendFileMessage(String fileName) {  
  94.         this.createConnection();  
  95.         try {  
  96.             int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;// 建立打开方式  
  97.   
  98.             queue = qManager.accessQueue(remoteQName, openOptions, nullnull,  
  99.                     null);// 连接队列(发送时此队列为发送方的远程队列)  
  100.   
  101.             MQPutMessageOptions pmo = new MQPutMessageOptions();// 创建消息放入方式实例  
  102.   
  103.             MQMessage message = new MQMessage();// 创建MQ消息实例  
  104.   
  105.             FileBean file = new FileBean();// 创建FileBean对象实例并赋值  
  106.   
  107.             file.setFileName(fileName);  
  108.   
  109.             InputStream in = new FileInputStream("D://" + fileName); // 输入流读取要发送的文件  
  110.   
  111.             BASE64Encoder encoder = new BASE64Encoder();// 创建BASE64Encoder编码实例  
  112.   
  113.             byte[] data = new byte[in.available()];  
  114.   
  115.             in.read(data);  
  116.   
  117.             String content = encoder.encode(data);// 编码文件 得到String  
  118.   
  119.             file.setFileContent(content);  
  120.   
  121.             message.writeObject(file);// 将FileBean实例 file放入消息中发送  
  122.   
  123.             queue.put(message, pmo);  
  124.   
  125.             qManager.commit();  
  126.             this.logInfo("文件发送成功");  
  127.         } catch (Exception e) {  
  128.             e.printStackTrace();  
  129.         } finally {  
  130.             this.closeAction();  
  131.         }  
  132.   
  133.     }  
  134.   
  135.     public void receiveFileMessage() {  
  136.         try {  
  137.             int openOptions = MQC.MQOO_INPUT_SHARED  
  138.                     | MQC.MQOO_FAIL_IF_QUIESCING;// 建立队列打开方式  
  139.   
  140.             queue = qManager.accessQueue(localQName, openOptions, nullnull,  
  141.                     null);// 连接队列(接收时队列名为接收方的本地队列)  
  142.   
  143.             MQGetMessageOptions gmo = new MQGetMessageOptions();  
  144.   
  145.             gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;// 同步接收  
  146.   
  147.             gmo.options = gmo.options + MQC.MQGMO_WAIT;// 没消息等待  
  148.   
  149.             gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING;// 停顿则失败  
  150.   
  151.             gmo.waitInterval = 100;// 等待间隔  
  152.   
  153.             MQMessage inMsg = new MQMessage();// 创建消息实例  
  154.   
  155.             queue.get(inMsg, gmo);// 从队列中拿出消息  
  156.   
  157.             FileBean fileBean = new FileBean();  
  158.   
  159.             fileBean = (FileBean) inMsg.readObject(); // 读取消息强转为FileBean类型  
  160.   
  161.             String content = fileBean.getFileContent();// 取文件内容  
  162.   
  163.             BASE64Decoder decoder = new BASE64Decoder();// 建立解码类实例  
  164.   
  165.             byte[] contentArray = decoder.decodeBuffer(content);// 解码生成byte数组  
  166.   
  167.             String path = "E://" + fileBean.getFileName();  
  168.   
  169.             FileOutputStream out = new FileOutputStream(new File(path));// 调动输出流把文件写到指定的位置  
  170.   
  171.             out.write(contentArray, 0, contentArray.length);  
  172.   
  173.             // System.out.print(fileBean.getFileName());  
  174.   
  175.             qManager.commit();// 提交事务  
  176.   
  177.             this.logInfo("文件接收成功,请注意查收");// 打印日志
  178.         } catch (Exception e) {  
  179.             e.printStackTrace();  
  180.         }  
  181.     }  
  182.   
  183.     /** 
  184.      *  
  185.      * Description: 释放资源
  186.      *  
  187.      * @param: 
  188.      * @return: 
  189.      * @exception Exception. 
  190.      * @author listening created at Nov 8, 2009 
  191.      */  
  192.     public void closeAction() {  
  193.         try {  
  194.             if (queue != null) {  
  195.                 queue.close();  
  196.                 queue = null;  
  197.             } else if (qManager != null) {  
  198.                 qManager.close();  
  199.                 qManager = null;  
  200.             }  
  201.         } catch (Exception e) {  
  202.             e.printStackTrace();  
  203.         }  
  204.   
  205.     }  
  206.   
  207.     /** 
  208.      *  
  209.      * Description:打印成功日志信息 
  210.      *  
  211.      * @param:String message-日志内容 
  212.      * @return: void 
  213.      * @exception Exception. 
  214.      * @author listening created at Nov 8, 2009 
  215.      */  
  216.     public void logInfo(String message) {  
  217.         SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");  
  218.         System.out.println(format.format(new Date()) + "-------" + message  
  219.                 + "+-------------");  
  220.     }  
  221.   
  222.     /** 
  223.      *  
  224.      * Description:main函数测试 
  225.      *  
  226.      * @param: 
  227.      * @return: void 
  228.      * @exception Exception. 
  229.      * @author listening created at Nov 8, 2009 
  230.      */  
  231.     public static void main(String[] args) {  
  232.         new MQSendAndReceiveUtil().sendFileMessage("test.xml");  
  233.         //new MQSendAndReceiveUtil().receiveFileMessage();  
  234.     }  
  235.   
  236. }  
package com.test.mq;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

/**
 * 
 * <p>
 * Title: MQSendAndReceiveUtil.java
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: shunde
 * </p>
 * 
 * @author: listening
 * @create date Nov 8, 2009
 */
public class MQSendAndReceiveUtil {
	private MQQueueManager qManager;
	private MQQueue queue;

	private static String qmManager = "QM_00000000";// 队列管理器名称
	private static String remoteQName = "RQ_88888888";// 远程队列名称
	private static String localQName = "LQ_00000000";// 本地队列
	private static String hostname = "192.168.1.66";// 本机名称
	private static String channel = "DC.SVRCONN";// 服务器链接通道
	private static int ccsid = 1381;
	private static int port = 1414;

	@SuppressWarnings("unchecked")
	private MQSendAndReceiveUtil() {
		MQEnvironment.hostname = hostname;
		MQEnvironment.channel = channel;
		MQEnvironment.CCSID = ccsid;
		MQEnvironment.port = port;
		MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,
				MQC.TRANSPORT_MQSERIES);

		try {
			qManager = new MQQueueManager(qmManager);
		} catch (MQException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * Description:如果队列管理器为空,建立
	 * 
	 * @param:
	 * @return: void
	 * @exception Exception.
	 * @author listening created at Nov 8, 2009
	 */
	private void createConnection() {
		if (qManager == null) {
			new MQSendAndReceiveUtil();
		}
	}

	
	/**
	 * 
	 * Description:发送文件
	 * 
	 * @param:String fileName -文件名
	 * @return: void
	 * @exception Exception.
	 * @author listening created at Nov 8, 2009
	 */
	public void sendFileMessage(String fileName) {
		this.createConnection();
		try {
			int penOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;// 建立打开方式

			queue = qManager.accessQueue(remoteQName, openOptions, null, null,
					null);// 连接队列(发送时此队列为发送方的远程队列)

			MQPutMessageOptions pmo = new MQPutMessageOptions();// 创建消息放入方式实例

			MQMessage message = new MQMessage();// 创建MQ消息实例

			FileBean file = new FileBean();// 创建FileBean对象实例并赋值

			file.setFileName(fileName);

			InputStream in = new FileInputStream("D://" + fileName); // 输入流读取要发送的文件

			BASE64Encoder encoder = new BASE64Encoder();// 创建BASE64Encoder编码实例

			byte[] data = new byte[in.available()];

			in.read(data);

			String content = encoder.encode(data);// 编码文件 得到String

			file.setFileContent(content);

			message.writeObject(file);// 将FileBean实例 file放入消息中发送

			queue.put(message, pmo);

			qManager.commit();
			this.logInfo("文件发送成功");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeAction();
		}

	}

	public void receiveFileMessage() {
		try {
			int penOptions = MQC.MQOO_INPUT_SHARED
					| MQC.MQOO_FAIL_IF_QUIESCING;// 建立队列打开方式

			queue = qManager.accessQueue(localQName, openOptions, null, null,
					null);// 连接队列(接收时队列名为接收方的本地队列)

			MQGetMessageOptions gmo = new MQGetMessageOptions();

			gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;// 同步接收

			gmo.options = gmo.options + MQC.MQGMO_WAIT;// 没消息等待

			gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING;// 停顿则失败

			gmo.waitInterval = 100;// 等待间隔

			MQMessage inMsg = new MQMessage();// 创建消息实例

			queue.get(inMsg, gmo);// 从队列中拿出消息

			FileBean fileBean = new FileBean();

			fileBean = (FileBean) inMsg.readObject(); // 读取消息强转为FileBean类型

			String content = fileBean.getFileContent();// 取文件内容

			BASE64Decoder decoder = new BASE64Decoder();// 建立解码类实例

			byte[] contentArray = decoder.decodeBuffer(content);// 解码生成byte数组

			String path = "E://" + fileBean.getFileName();

			FileOutputStream ut = new FileOutputStream(new File(path));// 调动输出流把文件写到指定的位置

			out.write(contentArray, 0, contentArray.length);

			// System.out.print(fileBean.getFileName());

			qManager.commit();// 提交事务

			this.logInfo("文件接收成功,请注意查收");// 打印日志
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * Description: 释放资源
	 * 
	 * @param:
	 * @return:
	 * @exception Exception.
	 * @author listening created at Nov 8, 2009
	 */
	public void closeAction() {
		try {
			if (queue != null) {
				queue.close();
				queue = null;
			} else if (qManager != null) {
				qManager.close();
				qManager = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * Description:打印成功日志信息
	 * 
	 * @param:String message-日志内容
	 * @return: void
	 * @exception Exception.
	 * @author listening created at Nov 8, 2009
	 */
	public void logInfo(String message) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
		System.out.println(format.format(new Date()) + "-------" + message
				+ "+-------------");
	}

	/**
	 * 
	 * Description:main函数测试
	 * 
	 * @param:
	 * @return: void
	 * @exception Exception.
	 * @author listening created at Nov 8, 2009
	 */
	public static void main(String[] args) {
		new MQSendAndReceiveUtil().sendFileMessage("test.xml");
		//new MQSendAndReceiveUtil().receiveFileMessage();
	}

}



至此,文件发送接收完成。

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值