flex 与 后台通讯

public function submit():void{

    

    //要请求的URL

    var request:URLRequest = new URLRequest("http://localhost:8086/test.do") ;

    var load:URLLoader = new URLLoader() ;

    //URL参数

    var variables:URLVariables = new URLVariables();

    

    //variables.+后面的参数 表示要提交的参数。

    variables.content = content.text;

    

    variables.no = 1001 ;

    //提交的方式

    request.method=URLRequestMethod.POST;

    

    //提交的数据

    request.data=variables ;

    

    load.dataFormat = URLLoaderDataFormat.TEXT ;

    load.load(request) ;

    

   }

后台通过:

String content = request.getParameter("content") 获取;

String no = request.getParameter("no ") 获取;




var request:URLRequest = new URLRequest(url);  

var urlVar:URLVariables = new URLVariables();  

urlVar.param = valStr;  

request.data = urlVar;  

request.method = URLRequestMethod.POST; 

 

 

 

 

 

 

通过Flex写上传 
我用FlieReference上传 URLRequest作为通信 

查过URLRequest 的api,通常情况下 
通过它的data属性来进行传值  可以使用ByteArray类型 URLVariables 再或者直接字符串 

API中写道 
ByteArray类型不支持POST 以及 FileReference.upload() , FileReference.download() 

我在网上找过例子使用URLVariables,代码如下 

Actionscript代码    收藏代码
  1. var request:URLRequest = new URLRequest(url);  
  2. var urlVar:URLVariables = new URLVariables();  
  3.  urlVar.param = valStr;  
  4.  request.data = urlVar;  
  5.  request.method = URLRequestMethod.POST;  


其中param作为属性名或者说是一个key 而valStr作为value 

而后台使用  (String)request.getParameter("param");来接受 
但挑了很长时间,不管用 后来也不知道是为什么 

看了看API 标明如下 
如果该对象是 URLVariables 对象,并且该方法是 POST,则使用 x-www-form-urlencoded 格式对变量进行编码,并且生成的字符串会用作 POST 数据。一种例外情况是对 FileReference.upload() 的调用,在该调用中变量将作为 multipart/form-data 发布中的单独字段进行发送。 

Java代码    收藏代码
  1. DiskFileItemFactory factory = new DiskFileItemFactory();  
  2. factory.setSizeThreshold(4096);  
  3. ServletFileUpload upload = new ServletFileUpload(factory);  
  4. upload.setSizeMax(maxPostSize);  
  5. try {  
  6.     List fileItems = upload.parseRequest(request);  
  7.     Iterator iter = fileItems.iterator();  
  8.     while (iter.hasNext()) {  
  9.         FileItem item = (FileItem) iter.next();  
  10.         if(item.getFieldName().equals("param")) {  
  11.                 item.getString();  
  12.                 ........  


正常的循环会循环出很多参数,所以需要判断 

这样就可以获取到参数值了  

 

Flex中Image控件相关之Java+Flex项目中ByteArray传到服务器端(java端)持久化(文件上传)<?xml version="1.0" encoding="utf-8"?>

Flex 代码:

<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 stub

                fileRefer.browse([new FileFilter("Images(*.jpg;*.gif;*.png)","*.jpg;*.gif;*.png")]);

            }

            

            protected function btn_canvertToImage_clickHandler(event:MouseEvent):void

            {

                // TODO Auto-generated method stub

                if(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 stub

                urlRequest.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="==&gt;" 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="==&gt;持久化到硬盘" chromeColor="#1F7888" click="btn_persistence_clickHandler(event)"/>

    <s:Label x="34" y="167" width="75" height="17" text="点击选择图片"/>

    

</s:Application>

Java服务器端代码:

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 stub

        System.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 stub

        doGet(request, response);

    }

}


你可能感兴趣的:(Flex)