今天闲着的时候研究了一下直接将flex中生成的报表转化成成图片直接通二进制流的方式传到服务端然后保存。
经过研究发现和简单。。。不罗嗦上代码。。。。
这个Demo使用了BlazeDS,有需要的朋友可以自己搭建环境,也可联系我索要工程。
Flex:
<?xml version=”1.0″ encoding=”utf-8″?><mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”vertical” backgroundGradientAlphas=”[1.0, 1.0]” backgroundGradientColors=”[#FFFFFF, #C0C0C0]” width=”700″ height=”650″ fontSize=”14″> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.rpc.events.ResultEvent; import mx.core.Application; import flash.display.BitmapData; import mx.graphics.codec.JPEGEncoder; import mx.graphics.codec.PNGEncoder; import mx.collections.ArrayCollection; import mx.core.UIComponent; //将要保存的文件格式 public static const FORMAT_JPEG:uint = 0x00; public static const FORMAT_PNG:uint = 0x01; //文件扩展名 private static const EXT_JPEG:String = “.jpg”; private static const EXT_PNG:String = “.png”; //图标的初始化数据 [Bindable] public var expenses:ArrayCollection = new ArrayCollection([ {Month:”Jan”, Profit:2000, Expenses:1500, Amount:450}, {Month:”Feb”, Profit:1000, Expenses:200, Amount:600}, {Month:”Mar”, Profit:1500, Expenses:500, Amount:300} ]); //将图片按指定的格式保存 private function saveImage(comp:DisplayObject,format:uint):void{ //将可视组件转换为Bitmapdata var bmpd:BitmapData = new BitmapData(comp.width,comp.height); bmpd.draw(comp); //图片最终的而二进制数组(Bytearray) var imgByteArray:ByteArray; //保存文件的扩展名 var ext:String; //验证指定的保存格式,并按指定格式调用相应的编码器生成相应的二进制图片数组 switch(format){ case FORMAT_JPEG: ext = EXT_JPEG; var jpgenc:JPEGEncoder = new JPEGEncoder(80); imgByteArray = jpgenc.encode(bmpd); break; case FORMAT_PNG: ext = EXT_PNG; var pngenc:PNGEncoder = new PNGEncoder(); imgByteArray = pngenc.encode(bmpd); break; } server.saveIMG(imgByteArray,getNowTimestamp()+ext); } //获取一个时间戳作为保存图片的名字 private function getNowTimestamp():String{ var d:Date = new Date(); var tstamp:String = d.getFullYear().toString()+d.getMonth()+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds()+d.getMilliseconds(); return tstamp; } //点击保存按钮时的处理方法 private function onClickSave(e:MouseEvent):void{ saveImage(myChart,FORMAT_PNG); } private function onResultHandler(event:ResultEvent):void{ Alert.show(event.result.toString()); } ]]> </mx:Script> <mx:RemoteObject id=”server” destination=”saveIMG” result=”onResultHandler(event)” endpoint=”http://localhost/Demo/messagebroker/amf”/> <mx:Label text=”Save a chart snapshot” color=”#171717″ fontSize=”20″ fontWeight=”bold”/> <mx:Panel title=”Line Chart”> <mx:LineChart id=”myChart” dataProvider=”{expenses}” showDataTips=”true”> <mx:horizontalAxis> <mx:CategoryAxis dataProvider=”{expenses}” categoryField=”Month” /> </mx:horizontalAxis> <mx:series> <mx:LineSeries yField=”Profit” displayName=”Profit” /> <mx:LineSeries yField=”Expenses” displayName=”Expenses” /> </mx:series> </mx:LineChart> <mx:Legend dataProvider=”{myChart}”/> </mx:Panel> <mx:Button id=”save_btn” label=”Click to save a snapshot” width=”181″ click=”onClickSave(event)”/></mx:Application>
Java:
package com.eunut.demo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class IMGServer {
public String saveIMG(byte [] io,String fileName){
BASE64Encoder encode = new BASE64Encoder();
String base64 = encode.encode(io);
if(generateImage(base64, "D:\\"+fileName))
return "图片保存成功!";
else
return "图片保存失败,请重新保存!";
}
// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
public static String GetImageStr(String imgFilePath) {
byte[] data = null;
// 读取图片字节数组
try {
InputStream in = new FileInputStream(imgFilePath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// 返回Base64编码过的字节数组字符串
}
// 对字节数组字符串进行Base64解码并生成图片
public static boolean generateImage(String imgStr, String imgFilePath) {
if (imgStr == null) // 图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] bytes = decoder.decodeBuffer(imgStr);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
// 生成图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(bytes);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}