您的位置:首页 > 其它

深入了解Mina框架中如何传送(消息+附件)实现方法

2013-05-02 17:25 429 查看
最近两天被IoBuffer搞的头晕目眩的,故单独写了个类测试。如果要学习mina框架,该博客有助于实现mina中的如何消息+附件的传送。

描述:模拟消息解析。

第一步:构建SmsObject对象

第二步:将对象设置到IoBuffer中;

第三步:从IoBuffer中将对象解析出来(解析结果包含附件);

经测试解析通过,注意代码如下

package com.dmis.mina.dx3;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.nio.charset.Charset;

import java.nio.charset.CharsetDecoder;

import java.nio.charset.CharsetEncoder;

import java.util.ArrayList;

import java.util.List;

import org.apache.mina.core.buffer.IoBuffer;

/**

* IoBuffer测试

*

* @author jrunner

*/

public class Test03 {

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

System.out.println("第一步:构建SmsObject对象");

List fileInfos = new ArrayList();

FileInfo f = new FileInfo();

File file = new File("c:\\test.txt");

f.setContent(file);

f.setFileName(file.getName());

f.setFileNameLength(f.getFileName().length());

f.setContentLength((int) file.length());

fileInfos.add(f);

f = new FileInfo();

file = new File("c:\\FeiQ.exe");

f.setContent(file);

f.setFileName(file.getName());

f.setFileNameLength(f.getFileName().length());

f.setContentLength((int) file.length());

fileInfos.add(f);

SmsObject sms = new SmsObject();

sms.setSender("158010122x");

sms.setReceiver("188696999x");

sms.setMessage("你好啊!");

sms.setFileInfos(fileInfos);

System.out.println("=========================================================");

System.out.println("第二步:将对象设置到IoBuffer中");

Charset cs = Charset.forName("UTF-8");

CharsetDecoder cd = cs.newDecoder();

CharsetEncoder ce = cs.newEncoder();

IoBuffer buffer = IoBuffer.allocate(100).setAutoExpand(true);

buffer.putString("http://jrunner.blog.51cto.com", ce);// 状态

buffer.putString(sms.getSender(), ce);// 发送者

buffer.putString(sms.getReceiver(), ce);// 接受者

buffer.putString(sms.getMessage(), ce);// 内容

buffer.putLong(89898989989898L);

for (int i = 0; i < sms.getFileInfos().size(); i++) {

FileInfo info = sms.getFileInfos().get(i);

buffer.putInt(info.getFileNameLength());// 设置文件名长度

buffer.putInt(info.getContentLength());// 设置文件大小

buffer.putString(info.getFileName(), ce);// 设置文件名

buffer.put(inputStreamToByte(new FileInputStream(info.getContent())));// 设置文件内容

}

System.out.println("第三步:从IoBuffer中将对象解析出来");

buffer.flip();

System.out.println(buffer.limit());

System.out.println(buffer.getString("http://jrunner.blog.51cto.com".getBytes().length+sms.getSender().getBytes().length+sms.getReceiver().getBytes().length + sms.getMessage().length() * 3, cd));

System.out.println(buffer.getLong());

System.out.println("------------------");

for (int i = 0; i < sms.getFileInfos().size(); i++) {

System.out.println("解析文件中...");

int L1 = buffer.getInt();// 文件名称长度

System.out.println("File Name Length:" + L1);

int L2 = buffer.getInt();// 文件内容长度

System.out.println("File Content Length:" + L2);

String fileName = buffer.getString(L1, cd);

System.out.println("FileName:" + fileName);

byte[] data = new byte[L2];

int k = 0;

while (buffer.hasRemaining()) {

data[k] = buffer.get();

k++;

if (k == L2) {

k = 0;

break;

}

}

File fs = new File("d:\\002_" + fileName);

OutputStream out = new FileOutputStream(fs);

out.write(data);

out.flush();

out.close();

}

}

public static byte[] inputStreamToByte(InputStream inStream) throws IOException {

ByteArrayOutputStream swapStream = new ByteArrayOutputStream();

int rc;

while ((rc = inStream.read()) != -1) {

swapStream.write(rc);

}

byte[] in2b = swapStream.toByteArray();

return in2b;

}

}

======================================================

public class FileInfo {

private File content;

private String fileName;

private int contentLength;

private int fileNameLength;

}

public class SmsObject {

private String sender;// 短信发送者

private String receiver;// 短信接受者

private String message;// 短信内容

private List<FileInfo> fileInfos;

}
本文出自 “艾客” 博客,请务必保留此出处http://jrunner.blog.51cto.com/1015356/1191697
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐