flex传图片到服务器(将流进行编码成string)


1. 将流和图片的名字同时传到后台,需要对流进行base64编码,将byte数组转换成string,
在jsp页面进行转码,中文乱码用encodeURIComponent,编码,后台jsp用URLDecoder.decode转成utf-8,
js中文图片显示:realName=encodeURI(realName ).replace(new RegExp("%","gm"), "-");,程序中同样处理

2. felx页面
var bitmapData:BitmapData = new BitmapData(620,420); 
var mat:Matrix=new Matrix();
mat.scale(620/Flow.flowDesignerArea.width,420/Flow.flowDesignerArea.height);
//mat.scale(620/Flow.flowDesignerArea.width,420/Flow.flowDesignerArea.height);
bitmapData.draw(Flow.flowDesignerArea,mat);

var bitmap : Bitmap = new Bitmap(bitmapData);  

var jpg:JPEGEncoder = new JPEGEncoder();  

var bitmapdata:BitmapData= new BitmapData(450,500);


var ba:ByteArray = jpg.encode(bitmapData); var service:HTTPService= new HTTPService();
service.url="http://localhost:8080/XX/MyJPG.jsp";
// service.resultFormat="xml";
//转换中文解码
service.method="POST";

var obj:Object= new Object();

obj.fileName= encodeURIComponent(fileName);
var data:String= ba.toString();
//对流进行解码
var base64:Base64Encoder = new Base64Encoder;
base64.encodeBytes(ba);
obj.data= base64.toString();
service.addEventListener(ResultEvent.RESULT,function (event:ResultEvent):void{
Alert.show("保存成功:"+event.result as String,"提示");
});

service.addEventListener(FaultEvent.FAULT,function (event:FaultEvent):void{

Alert.show("保存失败","提示");
});
service.send(obj);

3.jsp页面处理保存到服务器上的项目里
<%@ page language="java" import="sun.misc.BASE64Decoder,java.io.*,java.util.*" pageEncoding="utf-8"%>
      <%
      BASE64Decoder decoder = new sun.misc.BASE64Decoder();   
     
           String fileName=request.getParameter("fileName");
           String dataString=request.getParameter("data");
           System.out.print(fileName);
            System.out.print(dataString);
          InputStream inputStream = request.getInputStream();  
         
          if(fileName!=null){
              byte [] bt=decoder.decodeBuffer(dataString);
             
              for(int i=0;i<bt.length;i++)
          {
                 if(bt[i]<0)
                 { //调整异常数据
              bt[i]+=256;
              }
           }
             
                      
                        File dir = new File(application.getRealPath("/") + "FlowOAImage");
    if (!dir.exists())
    {
      dir.mkdirs();
    }//中文图片显示
    System.out.println( application.getRealPath("/") + "FlowOAImage");
             String path = dir.getAbsolutePath() + "/"
+ fileName.replaceAll("%", "-");
          OutputStream  fos= new FileOutputStream(new File(path+".jpg"),false);
          fos.write(bt);
          fos.flush();
          fos.close();
    fileName=java.net.URLDecoder.decode(fileName,"utf-8");
        out.print(fileName+".jpg");
          }else {
        
          }
        
         %>

你可能感兴趣的:(String)