amchart导出图片(ssh实现)

amchart导出图片,网上全是java的例子,但都是采用的jsp+servlet方式。本文将在struts2框架下描述amchart实现导出图片。

只讲flex的图片导出,javascript版的amchart根据amchart官网介绍暂时没有提供相应的方法,也没有该计划。

1、在flex中添加导出图片的代码:

    

// Save as image //////////////////////////
			private function saveAsImage():void
			{
				var pngSource:BitmapData = new BitmapData (trendVbox.width, trendVbox.height);
				pngSource.draw(trendVbox);
				
				var pngEncoder:PNGEncoder = new PNGEncoder();
				var pngData:ByteArray = PNGEncoder.encode(pngSource); 
				
				var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
				var uRLRequest:URLRequest = new URLRequest("http:localhost:8080/common/exportChartAsImage.action?fileName=chart.png");
				uRLRequest.requestHeaders.push(header);
				uRLRequest.method = URLRequestMethod.POST;
				uRLRequest.data = pngData;
				navigateToURL(uRLRequest);				
			}
			
			
			private function contextMenuEventHandler(event:ContextMenuEvent):void
			{
				saveAsImage();            	
			}

红色部分代码为关键。

2、struts2后台代码:

     action代码:

       public String exportChartAsImage() throws IOException{
		setResultCode(SUCCESS);
		
		HttpServletRequest request=ServletActionContext.getRequest();
		this.setInputStream(request.getInputStream());
		
		return getResultCode();
	}
        public InputStream getInputStream() throws Exception {   
                return inputStream;   
        } 
        public void setInputStream(InputStream inputStream) {
               this.inputStream = inputStream;
        }
        private String fileName;
        private InputStream inputStream;
    
        public String getFileName() {
                return fileName;
        }

        public void setFileName(String fileName) {
               this.fileName = fileName;
        }



     struts.xml:
<!-- save chart as image -->
		<action name="exportChartAsImage" class="comparisonAction" method="exportChartAsImage">
			<result name="success" type="stream">
                              <param name="contentType">application/octet-stream</param>
                              <param name="inputName">inputStream</param>
                              <param name="contentDisposition">attachment;filename="${fileName}"</param>
                              <param name="bufferSize">4096</param>
                      </result>
		</action>



你可能感兴趣的:(exception,String,struts,Flex,ssh,header)