Explicitly mapping ActionScript and Java objectsãçºãã¦ããã Java ã® java.lang.Byte[] 㨠ActionScript ã® flash.utils.ByteArray ã§åã®å¯¾å¿é¢ä¿ã¤ãã¦ã¾ãããã¨ã®ãã¨ãFlex-Java ã®ãã¡ã¤ã«ã¢ãããã¼ãã»ãã¦ã³ãã¼ãã£ã¦ããã§ããã®ããªï¼ãã¨æã£ãã®ã§ã¡ãã£ã¨å®é¨ãã¦ã¿ãã
ã¨ããããã½ã¼ã¹ã³ã¼ãã
Java å´ã¯ãããªæãã
public class FileDto { public String fileName; public Byte[]fileContents; }
public class FileService { public FileDto download() throws Exception { File dlFile = new File("bg_h1.jpg"); byte[] buf = new byte[(int) dlFile.length()]; InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(dlFile)); in.read(buf); //ä¾å¤ã¯ãã§ã³ããã¦å ¨ã¹ã«ã¼ } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } FileDto fileDto = new FileDto(); fileDto.fileName = dlFile.getName(); fileDto.fileContents = org.apache.commons.lang.ArrayUtils.toObject(buf); return fileDto; } public void upload(FileDto fileDto) throws Exception { System.out.println("upload file name:"+fileDto.fileName+ " size:" +fileDto.fileContents.length); File upFile = new File("upload/"+fileDto.fileName); byte[] buf = org.apache.commons.lang.ArrayUtils.toPrimitive(fileDto.fileContents); OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(upFile)); out.write(buf); } finally { out.close(); } } }
Flex å´ã¯ãããªæãã
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"> <mx:Script> <![CDATA[ import mx.core.ByteArrayAsset; import mx.rpc.events.FaultEvent; import dto.FileDto; [Embed(source='bg_h1.jpg', mimeType="application/octet-stream")] private var Image0 : Class; private function downloadResultHandler():void { var fileDto:FileDto = fileSrv.download.lastResult; fileName.text = fileDto.fileName; var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function (event: Event): void { fileImage.data = loader.content; }); loader.loadBytes(fileDto.fileContents); trace(fileDto.fileContents.length); } private function uploadResultHandler():void { trace("upload result"); } private function downloadButtonHandler():void { fileSrv.download(); } private function uploadButtonHandler():void { trace("uploadButtonHandler"); var storyByteArray:ByteArrayAsset = ByteArrayAsset(new Image0()); var fileDto:FileDto = new FileDto(); trace(Image0.toString()); fileDto.fileName = "bg_h1.jpg"; fileDto.fileContents = storyByteArray; fileSrv.upload(fileDto); } private function downloadImageClearButtonHandler():void { fileImage.data = null; } private function faultHandler(event:FaultEvent):void { trace("faultHandler"); errorText.text = event.fault.toString(); } ]]> </mx:Script> <mx:RemoteObject id="fileSrv" destination="fileService" > <mx:method name="download" result="downloadResultHandler()" fault="faultHandler(event)"/> <mx:method name="upload" result="uploadResultHandler()" fault="faultHandler(event)"/> </mx:RemoteObject> <mx:HBox width="100%"> <mx:Button id="downloadButton" label="download" click="downloadButtonHandler()"/> <mx:Button id="imageClear" label="download image clear" click="downloadImageClearButtonHandler()"/> <mx:Button id="uploadButton" label="upload" click="uploadButtonHandler()"/> </mx:HBox> <mx:VBox width="100%"> <mx:Text id="fileName" text="ã¦ãã¨" width="100%" height="100%" fontSize="21" fontWeight="bold"/> <mx:Image id="fileImage" alpha="1.0"/> </mx:VBox> <mx:TextArea id="errorText" width="496" height="82"/> </mx:Application>
ãã¦ãçªã£è¾¼ã¿ã©ããå¤ã ãªã®ã§ãç解ãã¦ãç¯å²ã§ã²ã¨ã¤ãã¤æ´çã
åãããã³ã°
Java 㧠Byte[]ãããªããã£ãã® byte[]ã«ããããã«ã¯ã¶ã¯ã¶åå¤æãè¡ã£ã¦ãããããFileDto ã® Byte[]㯠byte[]ã«ãã¦ãåä½ãããByte[]ãããªãã¨åä½ãã¦ãããªãã¨æã£ããã© RemoteObject コンポーネントの使用 ãè¦ãã¨ãJava 㨠AS3 ã®ãããã³ã°ã¯ byte[] 㨠ByteArray ã«ãªã£ã¦ããã ããããã©ããã¼ã¯ã©ã¹ã¨ããªããã£ãåãã©ã£ã¡ã§ãåä½ãããã£ã¦ãã¨ãªã®ããªï¼ï¼ã¾ã¨ãã«ããã¥ã¡ã³ãèªãã§ãªãã®ã§ã©ã£ãã«æ¸ãã¦ãããããï¼ãã¾ãããããããã¨ããã°ãããããã
ãµã¼ãã¹ã¯ã©ã¹ã®ä½ç½®ï¼
ä¸è¨ã®ãããªã³ã¼ãã FileService#upload ãªã©ã®ãµã¼ãã¹ã¡ã½ããã§å®è¡ããã¨ä½ã表示ãããã®ããªï¼ãã¨æã£ã¦ãã£ã¦ã¿ãã
File f = new File(""); System.out.println(f.getAbsolutePath());
ã³ã³ããã¹ãã«ã¼ãããªã¼ã¨æããã TOMCAT_HOME ã®ãã£ã¬ã¯ããªåã表示ããããã¡ã¨äºæ³å¤â¦â¦ããªãã§ã³ã³ãªãã ãï¼
Flex 㧠ByteArray ã Image ã«è¡¨ç¤ºããã
ä½æ°ã«æ©ãã ç®æãªã®ã§ã¡ã¢ã
ä¸ã æ å ±ãå°ãªãã¦å°ãã¾ããï¼
はてなダイアリー ããæç²
ãããã§é常ã«å©ããã¾ããï½
Flex ã§ããã¦ã®ãã¡ã¤ã«ãã¦ã³ãã¼ãã¨ã¢ãããã¼ã
ç»å表示㯠Image#load() ã¨ããããã¼ã¯ flash.net.URLRequest ã¨ã flash.net.FileReference ã¨ãããããã§ã
ã§ãæã£ããã¨ããµã³ãã«ã³ã¼ãã§å®é¨ãããããªããæ¹ã£ã¦å¦¥å½ãªã®ããªï¼ãã¨ããç¹ããã¨ãã°ãã¢ãããã¼ãã®å ´åãã¼ã«ã«ãã¡ã¤ã«ã ByteArray ã«å ¥ãããã ãã©ãããã£ã¦ã¡ã¢ãªæ¶è²»ããã«ã«ãªããªãå¯è½æ§ãããããã§â¦â¦
ãã®ã³ã¼ããå³å¯ã«ã¯ã¢ãããã¼ããã¦ãã£ã¦è¨ããªããªãï¼
Yes. ææãã®ãã swf ã«åãè¾¼ãã ç»åãã¡ã¤ã«ããµã¼ãã«éãè¿ãã¦ãã ãã§ãï½
ã¨ããã®ã¯å»ºåã§ããã³ãã¼ã¯ Flash Player ããã ã¨ç°¡åã«ãã¼ã«ã«ãã¡ã¤ã«è¦ããªãã¿ãããªãã ãããè²ã 調ã¹ã¦ã¿ã㨠AIR ではこんな感じ ã§ãããã FlashPlayer10からローカルアクセスできます! ã£ã¦æ å ±ããã»ãã¥ãªãã£çã«ã¢ã¬ãªã®ã§è¦ããªãããã«ãã¦ãããããã
ãªã®ã§ãä»ã®ã¨ãã Flex 使ã£ãéçºã§ãã¼ã«ã«ãªã½ã¼ã¹ã«ãã·ãã·ã¢ã¯ã»ã¹ãããå ´å㯠AIR 使ã£ãã»ããããããã§ããªã