氟塑料離心泵www.buybeng.com

          jquery教程http://www.software8.co/wzjs/jquery/

          深入了解Mina框架中如何傳送(消息+附件)實(shí)現(xiàn)方法

          最近兩天被IoBuffer搞的頭暈?zāi)垦5?,故單?dú)寫了個類測試。如果要學(xué)習(xí)mina框架,該博客有助于實(shí)現(xiàn)mina中的如何消息+附件的傳送。
          描述:模擬消息解析。
          第一步:構(gòu)建SmsObject對象
          第二步:將對象設(shè)置到IoBuffer中;
          第三步:從IoBuffer中將對象解析出來(解析結(jié)果包含附件);
          經(jīng)測試解析通過,注意代碼如下
          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("第一步:構(gòu)建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("第二步:將對象設(shè)置到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);// 狀態(tài)
            buffer.putString(sms.getSender(), ce);// 發(fā)送者
            buffer.putString(sms.getReceiver(), ce);// 接受者
            buffer.putString(sms.getMessage(), ce);// 內(nèi)容
            buffer.putLong(89898989989898L);
            for (int i = 0; i < sms.getFileInfos().size(); i++) {
             FileInfo info = sms.getFileInfos().get(i);
             buffer.putInt(info.getFileNameLength());// 設(shè)置文件名長度
             buffer.putInt(info.getContentLength());// 設(shè)置文件大小
             buffer.putString(info.getFileName(), ce);// 設(shè)置文件名
             buffer.put(inputStreamToByte(new FileInputStream(info.getContent())));// 設(shè)置文件內(nèi)容
            }
            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();// 文件內(nèi)容長度
             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;// 短信發(fā)送者
           private String receiver;// 短信接受者
           private String message;// 短信內(nèi)容 
           private List<FileInfo> fileInfos;
          }

          posted on 2013-05-03 10:25 你爸是李剛 閱讀(225) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2013年5月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          技術(shù)網(wǎng)站

          行業(yè)網(wǎng)站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          站長網(wǎng) 氟塑料離心泵 注塑機(jī) 液晶廣告機(jī)
          主站蜘蛛池模板: 福泉市| 娄烦县| 辛集市| 甘谷县| 锡林郭勒盟| 木兰县| 鹿泉市| 澜沧| 巴楚县| 文登市| 宜宾县| 家居| 余江县| 荥阳市| 阿瓦提县| 鲁山县| 府谷县| 巴里| 泰和县| 广南县| 花莲市| 玛纳斯县| 文成县| 海安县| 博野县| 晋州市| 石柱| 金塔县| 平利县| 阳西县| 沂南县| 虹口区| 临清市| 寻乌县| 博乐市| 周口市| 义乌市| 合阳县| 营口市| 长宁县| 东城区|