ActiveMq实现图片传输的过程。
来源:互联网 发布:淘宝主店铺旺旺怎么找 编辑:程序博客网 时间:2024/04/30 22:02
最近在学习activeMq,关于很多的文章卧我相信很多想去了解这个消息队列的人都会去看,先是看的zeroMq,目前用的就是zeroMq.但是为了不局限雨zeroMq,于是自己学习了这个。
这里简单的做了消息的传输和图片的传输:
相信关于activeMQ的安装已经很多啦!我这里用的是apache-activemq-5.9.0-bin这个版本的,下载安装。
这里我就不详细说了。
我这里主要处理的是信息接收和图片转化发送:
发送者:
package com.foxx;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.imageio.ImageIO;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 消息发送者
*/
public class SenderHead {
// 发送次数
public static final int SEND_NUM = 5;
// tcp 地址
public static final String BROKER_URL = "tcp://localhost:61616";
// 目标,在ActiveMQ管理员控制台创建 http://localhost:8161/admin/queues.jsp
public static final String DESTINATION = "hoo.mq.queue";
static BASE64Encoder encoder = new sun.misc.BASE64Encoder();
static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
public static void sendMessage(Session session, MessageProducer producer) throws Exception {
File f = new File("D:/timg.jpg");
BufferedImage bi;
try {
bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos); //经测试转换的图片是格式这里就什么格式,否则会失真
byte[] bytes = baos.toByteArray();
String mes= encoder.encodeBuffer(bytes).trim()+"图片";
// for (int i = 0; i < 100; i++) {
// String message = "发送消息第" + (i + 1) + "条";
TextMessage text = session.createTextMessage(mes);
System.out.println(mes);
producer.send(text);
// }
}catch (Exception e) {
System.out.println(e.toString());
}
}
public static void run() throws Exception {
Connection connection = null;
Session session = null;
try {
ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);
connection = factory.createConnection();
connection.start();
// 创建一个session会话
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 创建一个消息队列
Destination destination = session.createQueue(DESTINATION);
// 创建消息制作者
MessageProducer producer = session.createProducer(destination);
// 设置持久化模式
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
sendMessage(session, producer);
// 提交会话
session.commit();
} catch (Exception e) {
throw e;
} finally {
// 关闭释放资源
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
}
}
public static void main(String[] args) throws Exception {
SenderHead.run();
}
}
接受者:
package com.foxx;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import sun.misc.BASE64Decoder;
public class MessageReceiver {
// tcp 地址
public static final String BROKER_URL = "tcp://localhost:61616";
// 目标,在ActiveMQ管理员控制台创建 http://localhost:8161/admin/queues.jsp
public static final String DESTINATION = "hoo.mq.queue";
static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
public static void run() throws Exception {
Connection connection = null;
Session session = null;
try {
// 创建链接工厂
ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);
// 通过工厂创建一个连接
connection = factory.createConnection();
// 启动连接
connection.start();
// 创建一个session会话
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 创建一个消息队列
Destination destination = session.createQueue(DESTINATION);
// 创建消息制作者
MessageConsumer consumer = session.createConsumer(destination);
while (true) {
Message message = consumer.receive(1000 * 100);
TextMessage text = (TextMessage) message;
if (text != null) {
System.out.println("接收:" + text.getText());
} else {
break;
}
// 接收数据的时间(等待) 100 ms
try {
byte[] bytes1 = decoder.decodeBuffer(text.getText());
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
BufferedImage bi1 = ImageIO.read(bais);
File w2 = new File("H://QQ1.jpg");// 可以是jpg,png,gif格式
ImageIO.write(bi1, "jpg", w2);// 不管输出什么格式图片,此处不需改动
System.out.println("图片保存成功");
} catch (IOException e) {
e.printStackTrace();
}
}
// 提交会话
session.commit();
} catch (Exception e) {
throw e;
} finally {
// 关闭释放资源
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
}
}
public static void main(String[] args) throws Exception {
MessageReceiver.run();
}
}
- ActiveMq实现图片传输的过程。
- activemq 对象的传输
- 案例:演示TCP传输过程的实现
- activeMQ,spring的jms异步传输
- 数据包的传输过程
- 电子邮件的传输过程
- 电子邮件的传输过程
- TCP提供可靠传输的工作原理和实现过程
- 使用img src Base64 实现图片的传输和预览
- ActiveMQ的简单实现
- ActiveMQ的实现
- 图片Base64加密实现图片传输
- activeMQ-传输协议
- ActiveMQ故障转移传输
- activeMQ-传输协议
- js为加载图片的实现过程
- HTTP传输的基本过程
- 网络帧的传输过程
- 结构体优先队列自定义优先度
- Python格式化字符
- IntelliJ IDEA 2017破解方法.有效期2099年
- 浏览器的渲染原理简介
- Shell系列—Shell printf 命令
- ActiveMq实现图片传输的过程。
- TCP简单通讯
- 不用定时器和汇编语言,只用C语言实现精确无误的延时
- centos离线安装nginx
- 七夕节
- Python——str字符串和unicode字符串
- debian系统下安装ssh
- 使用OLAMI自然语言开放平台提供的API接口制作自己的语音助手
- SVN简单介绍