`
wang_ping001
  • 浏览: 87498 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ActiveMQ发送文件

阅读更多
  package com.unmi.jms;

  import java.io.File;

  import javax.jms.*;

  import javax.swing.JFileChooser;

  import org.apache.activemq.*;

  /**

  * 通过 ActiveMQ 发送文件的程序

  * @author Unmi

  */

  public class FileSender {

   /**

  * @param args

  * @throws JMSException

  */

  public static void main(String[] args) throws JMSException {

   // 选择文件

  JFileChooser fileChooser = new JFileChooser();

  fileChooser.setDialogTitle("请选择要传送的文件");

  if (fileChooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {

  return;

  }

  File file = fileChooser.getSelectedFile();

   // 获取 ConnectionFactory

  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(

  "tcp://10.80.38.10:61616?jms.blobTransferPolicy.defaultUploadUrl=http://10.80.38.10:8161/fileserver/");

   // 创建 Connection

  Connection connection = connectionFactory.createConnection();

  connection.start();

   // 创建 Session

  ActiveMQSession session = (ActiveMQSession) connection.createSession(

  false, Session.AUTO_ACKNOWLEDGE);

   // 创建 Destination

  Destination destination = session.createQueue("File.Transport");

   // 创建 Producer

  MessageProducer producer = session.createProducer(destination);

  producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);// 设置为非持久性

  // 设置持久性的话,文件也可以先缓存下来,接收端离线再连接也可以收到文件

   // 构造 BlobMessage,用来传输文件

  BlobMessage blobMessage = session.createBlobMessage(file);

  blobMessage.setStringProperty("FILE.NAME", file.getName());

  blobMessage.setLongProperty("FILE.SIZE", file.length());

  System.out.println("开始发送文件:" + file.getName() + ",文件大小:"

  + file.length() + " 字节");

   // 7. 发送文件

  producer.send(blobMessage);

  System.out.println("完成文件发送:" + file.getName());

   producer.close();

  session.close();

  connection.close(); // 不关闭 Connection, 程序则不退出

  }

  }

  

  3. 编写接收文件的程序 FileReceiver.java

  package com.unmi.jms;

  import java.io.*;

  import javax.jms.*;

  import javax.jms.Message;

  import javax.swing.*;

  import org.apache.activemq.*;

  public class FileReciever {

   /**

  * @param args

  * @throws JMSException

  */

  public static void main(String[] args) throws JMSException {

   // 获取 ConnectionFactory

  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(

  "tcp://10.80.38.10:61616");

   // 创建 Connection

  Connection connection = connectionFactory.createConnection();

  connection.start();

   // 创建 Session

  Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

   // 创建 Destinatione

  Destination destination = session.createQueue("File.Transport");

   // 创建 Consumer

  MessageConsumer consumer = session.createConsumer(destination);

   // 注册消息监听器,当消息到达时被触发并处理消息

  consumer.setMessageListener(new MessageListener() {

   // 监听器中处理消息

  public void onMessage(Message message) {

  if (message instanceof BlobMessage) {

  BlobMessage blobMessage = (BlobMessage) message;

  try {

  String fileName = blobMessage.getStringProperty("FILE.NAME");

  System.out.println("文件接收请求处理:" + fileName + ",文件大小:"

  + blobMessage.getLongProperty("FILE.SIZE")+ " 字节");

   JFileChooser fileChooser = new JFileChooser();

  fileChooser.setDialogTitle("请指定文件保存位置");

  fileChooser.setSelectedFile(new File(fileName));

  if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {

  File file = fileChooser.getSelectedFile();

  OutputStream os = new FileOutputStream(file);

   System.out.println("开始接收文件:" + fileName);

  InputStream inputStream = blobMessage.getInputStream();

   //写文件,你也可以使用其他方式

  byte[] buff = new byte[256];

  int len = 0;

  while ((len = inputStream.read(buff)) > 0) {

  os.write(buff, 0, len);

  }

  os.close();

  System.out.println("完成文件接收:" + fileName);

  }

   } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

  });

  }

  }

分享到:
评论

相关推荐

    JMS 使用 ActiveMQ 传送文件

    NULL 博文链接:https://itjiehun.iteye.com/blog/1321969

    activemq-web-console-5.11.2

    activemq-web-console的默认使用方式是通过在activemq.xml中导入jetty.xml配置一个jetty server来实现的...3.还有一个fileserver,用来支持通过activemq发送文件时的中转服务器。blob message时配置的http文件服务器。

    ACTIVEMQ C#下的例子

    activemq 传送数据流发送文件,仅供参考

    liuwei1989#study-guide#ActiveMQ传输文件的几种方式原理1

    对于比较小的文件,简单的处理方式是先读取所有的文件成byte[],然后使用ByteMessage,把文件数据发送到broker,像正常的message一样处理。

    ActiveMq+SpringMVC实现邮件异步发送

    ActiveMq整合SpringMVC实现批量邮件进行异步发送,下载后可在Eclipse中直接使用(含有ActiveMq安装文件已配置好解压即可用)希望对大家有所帮助

    activemq-cpp发送接收消息,消息过滤器

    本代码关于activemq-cpp的核心代码参考的chenxun2009的博客园,其他部分包括:从配置文件中读取消息通道,过滤条件等信息。

    自己写的ActiveMQ的Demo例子

    自己写的ActiveMQ简单demo,包括生产者、消费者之间发送消息、持久化到文件和持久化到数据库,期中持久化需要修改activemq.xml文件

    java+ftpServer+derby+ActiveMQ

    ftpSever\derby\ActiveMQ,将上传到ftp的信息进行获取发送到mq上。

    SpringBoot整合ActiveMQ(消息中间件)实现邮件发送功能

    SpringBoot整合ActiveMQ(消息中间件)实现邮件发送功能,里面含有详细业务逻辑代码,配置文件等

    ActiveMQ5.10下载以及交互例子

    该文件包含ActiveMQ5.10服务包,并且包含一个交互实例,发送者先发送信息再接收信息,接收端先接收信息再发送信息

    Vs2015ActiveMq测试工具.rar

    开发activemq发送程序,遇到问题 (1)Qt5AxContainer.lib error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“1”  Qt5AxBase.lib error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的...

    ActiveMQ实现Android端的消息推送,包含Android端和Server端的代码和使用说明

    1、解压安装文件,执行\apache-activemq-5.13.3-bin\apache-activemq-5.13.3\bin\win64\wrapper.exe. 2、测试发送消息,打开本地服务器地址http://localhost:8161,登录服务器,默认用户名密码:admin,admin。登录...

    batch:Spring JMS with ActiveMQ – 发送和接收消息示例

    Spring JMS with ActiveMQ – 发送和接收消息示例。 此代码假装是将 ActiveMQ 与 JMS Spring Beans 一起使用的简单概念证明。 这个想法是查找 Spring 上下文来处理 JMS bean。 配置 您需要配置一个名为 BATCH_CONF ...

    SpringBoot集成ActiveMQ

    SpringBoot集成ActiveMQ,ActiveMQ支持队列和主题两种消息发送方式,选择发送方式可以在SpringBoot的配置文件中通过参数spring.jms.pub-sub-domain来控制,值为false表示是队列,值为true表示是主题。

    ActiveMQ.rar

    基于ActiveMQ的多人聊天系统(c#,winform),实现功能:发送文本信息,发送图片,传输文件,好友列表,群聊私聊

    JMS实时消息通讯源码-ActiveMQ Java服务端和Android客户端源码

    1、解压安装文件,执行\apache-activemq-5.13.3-bin\apache-activemq-5.13.3\bin\win64\wrapper.exe. 2、测试发送消息,打开本地服务器地址http://localhost:8161,登录服务器,默认用户名密码:admin,admin。登录...

    ActiveMQ入门实例

    去官方网站下载:...创建project:ActiveMQ-5.5,并导入apache-activemq-5.5.1\lib目录下需要用到的jar文件,项目结构如下图所示最后接收者跟发送者在不同的机器上测试项目所引用的jar最后在Acti

    jms activeMQ 经典代码

    写spring配置文件的时候, 要把MessageProducer, MessageConsumer,MessageListener,MessageListenerContainer几个地方弄清楚: 1.可以有一个或者多个消息生产者向同一个destination发送消息. 2.queue类型的只能有一个...

    FileTransfer_文件上传_多线程_

    C# 多线程文件上传和下载工具源码(含FTP/SMTP/MSMQ/ActiveMQ的接收与发送)

Global site tag (gtag.js) - Google Analytics