- 浏览: 38984 次
- 性别:
- 来自: 上海
文章分类
最新评论
Websphere MQ 不多介绍,IBM的消息中间件。
新手初学MQ,高手勿笑。献丑了。
MQ 文件的传输
对于MQ传输文件,我采取的思路是:
A.先定义一个序列化类(赞命名为fileBean),类的属性有fileName和fileContent 两个。
B.用输入流配合 BASE64Encoder 将文件格式化为 基于BASE64Encoder 的String编码 作为文件的内容。
C.将文件名和文件内容set到fileBean的fileContent属性中。
D.调用MQ将这个Object写到远程队列中去。
E.接收方接受到消息时用readObject()方法读出,强转成fileBean
F.从fileBean中取出文件名和文件内容,将文件内容用BASE64Decoder解码
G.用文件输出流将文件写到指定的位置,到此大功告成。
-------------------------废话少说,看代码-------------------------------
文件序列化类
文件发送和接受类
至此,文件发送接收完成。
新手初学MQ,高手勿笑。献丑了。
MQ 文件的传输
对于MQ传输文件,我采取的思路是:
A.先定义一个序列化类(赞命名为fileBean),类的属性有fileName和fileContent 两个。
B.用输入流配合 BASE64Encoder 将文件格式化为 基于BASE64Encoder 的String编码 作为文件的内容。
C.将文件名和文件内容set到fileBean的fileContent属性中。
D.调用MQ将这个Object写到远程队列中去。
E.接收方接受到消息时用readObject()方法读出,强转成fileBean
F.从fileBean中取出文件名和文件内容,将文件内容用BASE64Decoder解码
G.用文件输出流将文件写到指定的位置,到此大功告成。
-------------------------废话少说,看代码-------------------------------
文件序列化类
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; } }
文件发送和接受类
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 openOptions = 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 openOptions = 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 out = 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(); } }
至此,文件发送接收完成。
评论
3 楼
mrcai314
2014-02-21
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
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_AAA";// 队列管理器名称
private static String remoteQName = "LQ_AAA";// 远程队列名称RQ_BBB
private static String localQName = "LQ_AAA";// 本地队列
private static String hostname = "169.254.12.138";// 本机名称
private static String channel = "DC.SVRCONN";// 服务器链接通道
private static int ccsid = 1381;
private static int port = 1414;
private static Hashtable properties = new Hashtable();
@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);*/
properties.put("hostname", "169.254.12.138");
properties.put("port", new Integer(1414));
properties.put("channel", "DC.SVRCONN");
properties.put("CCSID", new Integer(1381));
properties.put("transport", "MQSeries");
properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
try {
//qManager = new MQQueueManager(qmManager);
qManager = new MQQueueManager(qmManager,properties);
} 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 openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;// 建立打开方式
//int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE;
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("C:\\" + 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 openOptions = 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();// 取文件内容
System.out.println(content);
BASE64Decoder decoder = new BASE64Decoder();// 建立解码类实例
byte[] contentArray = decoder.decodeBuffer(content);// 解码生成byte数组
String path = "C:\\" + fileBean.getFileName();
OutputStream out = 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) {
//MQSendAndReceiveUtil mqsru = new MQSendAndReceiveUtil();
new MQSendAndReceiveUtil().sendFileMessage("spring-db.xml");
new MQSendAndReceiveUtil().receiveFileMessage();
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
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_AAA";// 队列管理器名称
private static String remoteQName = "LQ_AAA";// 远程队列名称RQ_BBB
private static String localQName = "LQ_AAA";// 本地队列
private static String hostname = "169.254.12.138";// 本机名称
private static String channel = "DC.SVRCONN";// 服务器链接通道
private static int ccsid = 1381;
private static int port = 1414;
private static Hashtable properties = new Hashtable();
@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);*/
properties.put("hostname", "169.254.12.138");
properties.put("port", new Integer(1414));
properties.put("channel", "DC.SVRCONN");
properties.put("CCSID", new Integer(1381));
properties.put("transport", "MQSeries");
properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
try {
//qManager = new MQQueueManager(qmManager);
qManager = new MQQueueManager(qmManager,properties);
} 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 openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;// 建立打开方式
//int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE;
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("C:\\" + 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 openOptions = 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();// 取文件内容
System.out.println(content);
BASE64Decoder decoder = new BASE64Decoder();// 建立解码类实例
byte[] contentArray = decoder.decodeBuffer(content);// 解码生成byte数组
String path = "C:\\" + fileBean.getFileName();
OutputStream out = 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) {
//MQSendAndReceiveUtil mqsru = new MQSendAndReceiveUtil();
new MQSendAndReceiveUtil().sendFileMessage("spring-db.xml");
new MQSendAndReceiveUtil().receiveFileMessage();
}
}
2 楼
mrcai314
2014-02-21
我也出上import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
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_AAA";// 队列管理器名称
private static String remoteQName = "LQ_AAA";// 远程队列名称RQ_BBB
private static String localQName = "LQ_AAA";// 本地队列
private static String hostname = "169.254.12.138";// 本机名称
private static String channel = "DC.SVRCONN";// 服务器链接通道
private static int ccsid = 1381;
private static int port = 1414;
private static Hashtable properties = new Hashtable();
@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);*/
properties.put("hostname", "169.254.12.138");
properties.put("port", new Integer(1414));
properties.put("channel", "DC.SVRCONN");
properties.put("CCSID", new Integer(1381));
properties.put("transport", "MQSeries");
properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
try {
//qManager = new MQQueueManager(qmManager);
qManager = new MQQueueManager(qmManager,properties);
} 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 openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;// 建立打开方式
//int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE;
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("C:\\" + 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 openOptions = 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();// 取文件内容
System.out.println(content);
BASE64Decoder decoder = new BASE64Decoder();// 建立解码类实例
byte[] contentArray = decoder.decodeBuffer(content);// 解码生成byte数组
String path = "C:\\" + fileBean.getFileName();
OutputStream out = 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) {
//MQSendAndReceiveUtil mqsru = new MQSendAndReceiveUtil();
new MQSendAndReceiveUtil().sendFileMessage("spring-db.xml");
new MQSendAndReceiveUtil().receiveFileMessage();
}
}
述异常,经修改验证通过后代码如下:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
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_AAA";// 队列管理器名称
private static String remoteQName = "LQ_AAA";// 远程队列名称RQ_BBB
private static String localQName = "LQ_AAA";// 本地队列
private static String hostname = "169.254.12.138";// 本机名称
private static String channel = "DC.SVRCONN";// 服务器链接通道
private static int ccsid = 1381;
private static int port = 1414;
private static Hashtable properties = new Hashtable();
@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);*/
properties.put("hostname", "169.254.12.138");
properties.put("port", new Integer(1414));
properties.put("channel", "DC.SVRCONN");
properties.put("CCSID", new Integer(1381));
properties.put("transport", "MQSeries");
properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
try {
//qManager = new MQQueueManager(qmManager);
qManager = new MQQueueManager(qmManager,properties);
} 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 openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;// 建立打开方式
//int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE;
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("C:\\" + 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 openOptions = 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();// 取文件内容
System.out.println(content);
BASE64Decoder decoder = new BASE64Decoder();// 建立解码类实例
byte[] contentArray = decoder.decodeBuffer(content);// 解码生成byte数组
String path = "C:\\" + fileBean.getFileName();
OutputStream out = 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) {
//MQSendAndReceiveUtil mqsru = new MQSendAndReceiveUtil();
new MQSendAndReceiveUtil().sendFileMessage("spring-db.xml");
new MQSendAndReceiveUtil().receiveFileMessage();
}
}
述异常,经修改验证通过后代码如下:
1 楼
spsace
2011-04-15
发送XML成功,但读取的时候报错:
public static void main(String[] args) {
//new MQSendAndReceiveUtil().sendFileMessage("MyEmployee.xml");
new MQSendAndReceiveUtil().receiveFileMessage();
}
异常如下:
java.io.StreamCorruptedException: invalid stream header: 58514820
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:782)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
at com.ibm.mq.MQObjectInputStream.<init>(MQObjectInputStream.java:145)
at com.ibm.mq.MQMessage.readObject(MQMessage.java:2018)
at ace.mq.utils.MQSendAndReceiveUtil.receiveFileMessage(MQSendAndReceiveUtil.java:159)
at ace.mq.utils.MQSendAndReceiveUtil.main(MQSendAndReceiveUtil.java:233)
请楼主帮忙解答。
public static void main(String[] args) {
//new MQSendAndReceiveUtil().sendFileMessage("MyEmployee.xml");
new MQSendAndReceiveUtil().receiveFileMessage();
}
异常如下:
java.io.StreamCorruptedException: invalid stream header: 58514820
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:782)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
at com.ibm.mq.MQObjectInputStream.<init>(MQObjectInputStream.java:145)
at com.ibm.mq.MQMessage.readObject(MQMessage.java:2018)
at ace.mq.utils.MQSendAndReceiveUtil.receiveFileMessage(MQSendAndReceiveUtil.java:159)
at ace.mq.utils.MQSendAndReceiveUtil.main(MQSendAndReceiveUtil.java:233)
请楼主帮忙解答。
相关推荐
一个简单的IBM MQ操作例子,put方法和get方法
IBM MQ大文件文件传输。相关程序.......
调用MQ向指定的队列中传输xml文件,同时发送成功后到指定的队列中去读取回复的xml格式的消息
基于MQ的文件分片传输系统设计与实现,吴成宾,朱彬,基于MQ的文件分片传输系统融合了通信中间件,Zlib压缩、对称加密等技术,能够实现异构环境下可靠的文件数据交换。本文介绍了分片文
利用Websphere MQ实现大文件交换,断点续传,可以将文件分割多段传输
MQ通道的创建/服务起停命令。unix环境MQ编程
如何利用MQ实现大文件传输和交换具有可靠、安全、断点传输等特点(java 实现概述)
本项目使用hibernate技术对用MQ传输的数据进行相关的入库,解析操作,如果只是想学习MQ的相关操作,可以吧里面的两个与MQ有关的类给拷贝出现,然后做学习之用。
您可以使用同业MQ安全、快捷、高效地与各地的旅游同行像用QQ一样实时文字聊天、音频聊天、视频聊天、传文件、建讨论组、建群,还可以在网上进行旅游产品发布和在线交易;另外,所有的交易结果都能通过同业MQ系统自动...
IBM MQ的运用越来越广泛,在部署运行过程中出来越来越多的问题,我们针对各种实际情况提供第三方工具,帮助用户解决一些实际问题,提高工作效率、简化操作流程,时及时发现、解决问题。... Ftp文件传输 实现远程管理
可管理的文件传输WebSphere MQ File Transfer Edition,文件传输的需求,IBM可管理的文件传输,架构设计与应用整合,
MQ应用程序的开发,简单的实现一下关键的发送消息和接收消息的操作.首先做简单的准备工作,建队列管理器QM1,在QM1中建队列Q1,然后在高级中建立通道BICASHY,类型为服务器连接通道,端口号1414,这里做最简单的配置...
HTTP或gRPC传输 聚类 分片 代理 发现 自动重试 TLS支持 命令行客户端 互动客户 去客户 坚持归档 如果未指定TLS配置,则MQ缺省情况下会生成自签名证书 原料药 发布 /pub?topic=string publish payload as body 订阅 ...
WebSphereMQ,也称MQSeries,以一致的、可靠的和易于管理的方式来连接应用程序,并为跨部门、企业范围的集成提供了可靠的基础。通过为重要的消息和事务提供可靠的、一次且仅一次的传递,MQ可以处理复杂的通信协议,...
WebSphere MQ 及其操作工具 WebSphere MQ ...你可以把 MQ 当成一个传输通道,只要往里面放东西,MQ 就会把消息传到目的 地。 WebSphere MQ Explorer 是用来操作 MQ 的工具,我们需要利用它来建立队列管理器及队列.
rebbitMQ压缩包 方便大家免费下载使用,这个 压缩包解压就可以使用,方便。
MQTT协议手册(中文版),MQ遥测传输(MQTT)是轻量级的、基于代理的发布/订阅消息传输协议,此协议的设计开放、简单、 轻量、易于实现。
本程序在Windows环境下运行,运行时需要ACED.dll(在"可执行文件及结果"目录下)支持。先运行MQServer.exe,再运行MQClient.exe。为了测试消息接受程序不在运行的情况,可以先运行 MQClient.exe,过几秒后再运行MQ...
FTE的配置及基本操作 IBM FTE是传输文件的目前最可靠高效的工具 需要MQ的支持
WebSphere MQ................................................................................... 17 1.1.3 WebSphere MQ 产品.......................................................................... ...