ADOS Developer's Site - XML Stadium |
【RJOによる受信した添付ファイルの保存】目次 概要 †アクションメッセージの受信時における、添付ファイルの参照方法について解説します。 サンプルプログラム全体を以下に示します。 【PIPReceiverAttach.java】 1 : import java.io.File; 2 : import java.io.FileInputStream; 3 : import java.io.FileWriter; 4 : import java.io.InputStream; 5 : import java.io.Writer; 6 : import java.util.Properties; 7 : 8 : import com.ados.hestia.parser.rnbm.exchange.Container; 9 : import com.ados.hestia.parser.rnbm.exchange.PayloadContainer; 10 : import com.ados.hestia.parser.rnbm.exchange.StatusContainer; 11 : import com.ados.hestia.parser.rnbm.particle.Attachment; 12 : import com.ados.hestia.sdk.Configuration; 13 : import com.ados.hestia.sdk.Connection; 14 : import com.ados.hestia.sdk.message.DirectMessageExchanger; 15 : import com.ados.hestia.sdk.session.RespondSession; 16 : import com.ados.hestia.sdk.session.Session; 17 : 18 : public class PIPReceiverAttach { 19 : 20 : public static void main(String[] args) { 21 : try { 22 : Properties prop = new Properties(); 23 : prop.load(new FileInputStream("sample.properties")); 24 : String HOST = prop.getProperty("HOST"); 25 : String USER = prop.getProperty("USER"); 26 : String PASS = prop.getProperty("PASS"); 27 : String HANDLER_ID = prop.getProperty("HANDLER_ID"); 28 : String DIR = System.getProperty("user.dir") + File.separator 29 : + "output" + File.separator + "attach"; 30 : 31 : Connection connection = new Connection(HOST, USER, PASS, 32 : Connection.EXCHANGE_MODE_DIRECT, null, null, null, 33 : HANDLER_ID); 34 : DirectMessageExchanger exchanger = new DirectMessageExchanger( 35 : connection, Configuration.getInstance()); 36 : 37 : new File(DIR).mkdirs(); 38 : int time = 0; 39 : boolean loop = true; 40 : while (loop) { 41 : Thread.sleep(5000); 42 : SampleUtils.println("loop : " + time++); 43 : exchanger.hit(); 44 : 45 : Container container = exchanger.receiveMessage(); 46 : if (container != null) { 47 : SampleUtils.printContainerInformation(container); 48 : PayloadContainer request = (PayloadContainer) container; 49 : SampleUtils.printServiceContent(request.getServiceContent()); 50 : Session session = new RespondSession(request 51 : .getRemotePartner(), request.getPIPID()); 52 : SampleUtils.printSessionState("created", session); 53 : session.pushMessage(request); 54 : SampleUtils.printSessionState("pushed", session); 55 : session.acceptPartnerMessage(); 56 : SampleUtils.printSessionState("accepted", session); 57 : StatusContainer status = session.getStatusToSend(); 58 : SampleUtils.printSessionState("sent status", session); 59 : SampleUtils.printContainerInformation(status); 60 : exchanger.sendMessage(status); 61 : exchanger.hit(); 62 : 63 : Attachment[] atts = request.getAttachments(); 64 : for (int i = 0; i < atts.length; i++) { 65 : Attachment att = atts[i]; 66 : InputStream in = att.openStream(); 67 : String filename = DIR + File.separator + att.getName(); 68 : Writer writer = new FileWriter(filename); 69 : int c; 70 : while ((c = in.read()) != -1) { 71 : writer.write(c); 72 : } 73 : in.close(); 74 : writer.close(); 75 : SampleUtils.println("Attachment : " + att.toString() 76 : + ",CID:" + att.getContentID()); 77 : } 78 : loop = false; 79 : } 80 : } 81 : 82 : } catch (Exception e) { 83 : e.printStackTrace(); 84 : } 85 : } 86 : 87 : } Attachmentの参照 †PayloadContainer#getAttachmentsによってAttachmentを取得できます。 63 : Attachment[] atts = request.getAttachments(); InputStreamから添付ファイルを読み込みます。サンプルではInputStreamにより文字を読み込み、Writerにより逐一出力しています。 66 : InputStream in = att.openStream(); 67 : String filename = DIR + File.separator + att.getName(); 68 : Writer writer = new FileWriter(filename); 69 : int c; 70 : while ((c = in.read()) != -1) { 71 : writer.write(c); 72 : } 73 : in.close(); 74 : writer.close(); 添付ファイルのファイル名やコンテントIDを参照できます。 75 : SampleUtils.println("Attachment : " + att.toString() 76 : + ",CID:" + att.getContentID()); |
Copyright 2005-2008. ADOS Co., Ltd. All Rights Reserved. |