我也是今天剛會(huì)的,flex4開發(fā)Chart圖表的初級(jí)程序員,不太會(huì)用MyEclipse建立servlet,這篇文章對(duì)你們很有用,程序代碼都是從網(wǎng)上搜集到的。
1.使用BitmapData將flex的某個(gè)UIComponent轉(zhuǎn)換為ByteArray 在mxml文件里編寫代碼,創(chuàng)建函數(shù)getJPGByteArray。 參數(shù):圖表對(duì)象實(shí)例 功能:將圖表轉(zhuǎn)換為ByteArray類型 配置:導(dǎo)入JPGEncoder.as和BitString.as類文件 2.使用JPGEncoder轉(zhuǎn)換ByteArray。 3.使用URLRequest發(fā)送ByteArray到服務(wù)端的java servlet 在mxml文件里編寫代碼,創(chuàng)建函數(shù)sendImageByte 參數(shù):圖表對(duì)象實(shí)例 功能:將轉(zhuǎn)換的數(shù)據(jù)發(fā)送到servlet地址 配置:導(dǎo)入JPGEncoder.as和BitString.as類文件 4.在servlet中使用ImageIO保存圖片或者輸出到response 編寫java文件,創(chuàng)建類ImageServlet繼承HttpServlet 功能:處理接收的ByteArray數(shù)據(jù)輸出jpg圖片 配置:導(dǎo)入commons-io-1.4.jar包 用到的包和類文件在附件里。 詳細(xì)解釋: 在flex4里創(chuàng)建mxml文件Keep_Jpg.mxml,代碼如下: < ?xml version="1.0" encoding="utf-8"?> < s:Application xmlns:fx="<a target="_blank">http://ns.adobe.com/mxml/2009<;;/a>" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Declarations> </fx:Declarations><br> <fx:Script><br> <![CDATA[ import mx.controls.Alert; import mx.core.UIComponent; import mx.collections.ArrayCollection; import com.JPGEncoder; [Bindable] public var expenses:ArrayCollection = new ArrayCollection( [ {Month:"Jan", Profit:2000, Expenses:1500}, {Month:"Feb", Profit:1000, Expenses:200}, {Month:"Mar", Profit:1500, Expenses:500} ] ); public function getJPGByteArray(target:UIComponent):ByteArray { var bitmapData : BitmapData = new BitmapData(target.width, target.height); bitmapData.draw(target); var jpg : JPGEncoder = new JPGEncoder(); var jpgByteArray : ByteArray = jpg.encode(bitmapData); return jpgByteArray; } public function sendImageByte(target:UIComponent):void { var request:URLRequest = new URLRequest("<a href="http://localhost:8080/Keep_images/requestUrl" target="_blank"><span style="color:#ff483f;">http://localhost:8080/Keep_images/requestUrl</span></a>"); request.contentType = 'applicatoin/octet-stream'; request.data = getJPGByteArray(target); request.method = URLRequestMethod.POST; navigateToURL(request, "_blank"); } ]]> </fx:Script><br> <mx:Panel id="chartPanel" title="Bar Chart" width="300" height="400"> <mx:BarChart id="myChart" dataProvider="{expenses}" width="100%" height="100%"> <mx:verticalAxis> <mx:CategoryAxis dataProvider="{expenses}" categoryField="Month" /> </mx:verticalAxis> <mx:series> <mx:BarSeries yField="Month" xField="Profit" displayName="Profit" /> <mx:BarSeries yField="Month" xField="Expenses" displayName="Expenses" /> </mx:series> </mx:BarChart> <mx:Legend dataProvider="{myChart}"/> <mx:Button label="導(dǎo)出圖表圖片" click="sendImageByte(myChart)"/> <mx:Button label="導(dǎo)出整個(gè)圖表圖片" click="sendImageByte(chartPanel)"/> </mx:Panel> </s:Application><br><strong> 在src文件夾里要?jiǎng)?chuàng)建com包,將JPGEncoder.as和BitString.as拷就去,這樣就不會(huì)出錯(cuò)了吧。 然后在MyEclipse里編寫Java代碼,創(chuàng)建Web Project,在src里創(chuàng)建包servlet,然后在包里創(chuàng)建ImageServlet.java文件,代碼如下: package servlet; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; public class ImageServlet extends HttpServlet { public ImageServlet() { super(); } public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { doService(arg0, arg1); } protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { doService(arg0, arg1); } public void init() throws ServletException { // Put your code here } private void doService(HttpServletRequest request, HttpServletResponse response) throws IOException { saveImage(request, response); //printImage(request, response); } private void saveImage(HttpServletRequest request, HttpServletResponse response) throws IOException { String fileName = "D://" + System.currentTimeMillis() + ".jpg"; BufferedImage bufferedImage = ImageIO.read(request.getInputStream()); if (bufferedImage != null) { ImageIO.write(bufferedImage, "jpeg", new File(fileName)); } } private void printImage(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("image/jpeg"); response.setHeader("Content-Length:", String.valueOf(request.getInputStream().available())); ServletOutputStream sos = response.getOutputStream(); IOUtils.copy(request.getInputStream(), sos); sos.flush(); sos.close(); } } 要導(dǎo)入包c(diǎn)ommons-io-1.4.jar,包放的文件目錄Keep_images\WebRoot\WEB-INF\lib里,然后點(diǎn)擊發(fā)布Project Deployments管理器,添加Keep_images到tomcat服務(wù)器, 還有一點(diǎn)修改WEB-INF里的web.xml文件哦!加入代碼如下(加到的上面): ImageServlet servlet.ImageServlet ImageServlet /requestUrl 然后運(yùn)行,地址http://localhost:8080/Keep_images/requestUrl,這個(gè)地址粘貼到那個(gè)mxml里就可以了。requestUrl的名字可以隨便起哦! 夠詳細(xì)了吧!再菜鳥的人也該會(huì)做了! 百圖匯-圖片設(shè)計(jì)資源 |
|