4、Flex中Image控件相关之Java+Flex项目中ByteArray传到服务器端(java端)持久化(文件上传)
Flex中Image控件相关之Java+Flex项目中ByteArray传到服务器端(java端)持久化(文件上传)<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="createHandler()" ><fx:Script><![CDATA[import mx.controls.Alert;import mx.utils.Base64Encoder;private function createHandler():void{fileRefer.addEventListener(Event.SELECT,selectHandler);//选择文件fileRefer.addEventListener(Event.COMPLETE,completeHandler);//加载完成}private function selectHandler(event:Event):void{if(fileRefer){fileRefer.load();}}private function completeHandler(event:Event):void{if(fileRefer){mySource.source = fileRefer.data;}}private var fileRefer:FileReference = new FileReference();protected function sourceFile_clickHandler(event:MouseEvent):void{// TODO Auto-generated method stubfileRefer.browse([new FileFilter("Images(*.jpg;*.gif;*.png)","*.jpg;*.gif;*.png")]);}protected function btn_canvertToImage_clickHandler(event:MouseEvent):void{// TODO Auto-generated method stubif(fileRefer.data){destSource.source = fileRefer.data;}}private var urlRequest:URLRequest = new URLRequest("http://localhost:9080/ByteArrayApp/persistence");protected function btn_persistence_clickHandler(event:MouseEvent):void{// TODO Auto-generated method stuburlRequest.data = fileRefer.data;urlRequest.contentType = "application/octet-stream";urlRequest.method = URLRequestMethod.POST;var urlloader:URLLoader = new URLLoader();urlloader.dataFormat = URLLoaderDataFormat.BINARY;urlloader.addEventListener(Event.COMPLETE,completePersistenceHandler);urlloader.load(urlRequest);}private function completePersistenceHandler(event:Event):void{Alert.show("持久化成功","恭喜");}]]></fx:Script><fx:Declarations></fx:Declarations><s:BorderContainer id="sourceFileContainer" x="8" y="30" borderColor="#1F7888" click="sourceFile_clickHandler(event)"><s:Image id="mySource" x="0" y="-4" width="130" height="127"/></s:BorderContainer><s:Button id="btn_canvertToImage" x="158" y="93" width="52" height="18" label="==>" chromeColor="#1F7888" color="#FEFDFD" click="btn_canvertToImage_clickHandler(event)"/><s:BorderContainer id="destSourceContainer" x="224" y="35" borderColor="#1F7888"><s:Image id="destSource" width="130" height="127"/></s:BorderContainer><s:Button id="btn_persistence" x="372" y="90" label="==>持久化到硬盘" chromeColor="#1F7888" click="btn_persistence_clickHandler(event)"/><s:Label x="34" y="167" width="75" height="17" text="点击选择图片"/></s:Application>
?package com.lmx.servlet;
import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.ServletInputStream;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class PersistenceServlet */@WebServlet("/persistence")public class PersistenceServlet extends HttpServlet {private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public PersistenceServlet() { super(); // TODO Auto-generated constructor stub }/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubSystem.out.println("============BEGIN===================");ServletInputStream servletInputStream = request.getInputStream();int len = request.getContentLength();byte buffer[] = new byte[len];int readlen=0;FileOutputStream fos = new FileOutputStream(new File("c:/jun.jpg"));while((readlen=servletInputStream.read(buffer))!=-1){fos.write(buffer, 0, readlen);}fos.flush();fos.close();servletInputStream.close();System.out.println("============END===================");}/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}?