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();
}
}
}
});
}
}
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();
}
}
}
});
}
}