现在的位置: 首页 > 综合 > 正文

flex端截图流传入java端生成gif图片

2018年01月29日 ⁄ 综合 ⁄ 共 3973字 ⁄ 字号 评论关闭

//flex端截图 传到服务器
			private function save():void{
				
				var target:UIComponent=map;
				var fr:FileReference = new FileReference();
				var bitmapData : BitmapData = new BitmapData(target.width,target.height);
				bitmapData.draw(target);
				var data:ByteArray = pngEnc.encode(bitmapData);
				
				var request:URLRequest = new URLRequest("http://localhost:8080/GISViewer/servlet/FileManagerSaveFileServlet?realPath="+encodeURIComponent(StringUtil.trim(realPath)));
				request.method=URLRequestMethod.POST;
				request.contentType = "application/octet-stream";
				request.data = data;
				var loader:URLLoader = new URLLoader();
				loader.load(request);
//				loader.addEventListener(Event.COMPLETE,saveResult);
			}

//java 端保存图片

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class FileManagerSaveFileServlet
 */
public class FileManagerSaveFileServlet extends HttpServlet {
 
 private int len=0;//处理流
 private int mm=10;//重命名
 private String fileName="";//文件原名
 private String extName="";//文件扩展名
 private String tempFileName="";//文件名加扩展名
 
 public void doGet(HttpServletRequest request, HttpServletResponse response)    
 throws ServletException, IOException {    
 processRequest(request, response);    
 }    
   
 public void doPost(HttpServletRequest request, HttpServletResponse response)    
  throws ServletException, IOException {    
 processRequest(request, response);    
 }    
 
 public void processRequest(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {
	  request.setCharacterEncoding("utf-8");
	  String realPath=request.getParameter("realPath");
	  //System.out.println("FMSFS-->realPath:"+realPath);
	  response.setContentType("application/octet-stream");
	  InputStream is = request.getInputStream();
	  try {
		  int size = 0;
		  byte[] tmp = new byte[100000];
		  
		  tempFileName=realPath.substring(realPath.lastIndexOf("\\")+1);//切割获得文件名加扩展名
		  fileName=tempFileName.substring(0,tempFileName.lastIndexOf("."));//切割获得文件名
		  //确保获得真实的文件名如:1(1)可以获得真实为1,
		  if(fileName.indexOf("(")!=-1){
			  fileName=fileName.substring(0,fileName.indexOf("("));
		  }
		 
		  
		  extName=tempFileName.substring(tempFileName.lastIndexOf("."));//切割获得扩展名
		  
		  mm++;
          str="_"+mm;
          fileName += str;
		  //调用递归方法
//		  fileName+=reNameFile(realPath.substring(0,realPath.lastIndexOf("\\")+1),fileName,extName);
		  // 创建一个文件夹用来保存发过来的图片;
		  
		  String newfileName = realPath.substring(0,26);
		  File file = new File(newfileName);
		  file.mkdirs();
		  
		  File f = new File(realPath.substring(0,realPath.lastIndexOf("\\")+1)+fileName+extName);
		  DataOutputStream dos = new DataOutputStream(new FileOutputStream(f));
		  while ((len = is.read(tmp)) != -1) {
			  dos.write(tmp, 0, len);
			  size += len;
		  }
		  dos.flush();
		  dos.close();
	  } catch (IOException e) {
		  e.printStackTrace();
	  }
 }
 
 //递归来重命名文件名
 String str="";
 public String reNameFile(String realPath,String filename,String extName){
	  File file =new File(realPath+"\\"+filename+extName);
	  str="";
        if(file.exists()){
	         mm++;
	         str="_"+mm;
	         reNameFile(realPath,fileName+str,extName);
        }else{
	         if(mm!=0){
		      str="_"+mm;
	         }
    	}
	  return str;
	 }
}

//java端 将保存的图片合成gif图

public ActionForward joinGIF(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String realPath=request.getParameter("realPath");
		 String newfileName = realPath.substring(0,26);
		 String newGIF =  realPath.substring(11,26) + ".gif";
		  File file=new File(newfileName);
		  
		  try{
			  String test[];
			  test=file.list();


			  String videoPath = getServlet().getServletContext().getRealPath("")+"/upload/gif/"+newGIF;
			  
			  //合成GIF:
			  AnimatedGifEncoder e = new AnimatedGifEncoder();
			  e.start(videoPath);
		 
			  for(int i=0;i<test.length;i++){
				   System.out.println(test[i]);
				   String imageFile = newfileName+"//"+test[i];
				   e.setDelay(1000);   // 1 frame per sec
				   BufferedImage bi = ImageIO.read(new File(imageFile));
				   e.addFrame(bi);
			  }
		     
			  e.finish();
		     
			  String str= "/upload/gif/"+newGIF;
		      response.setCharacterEncoding("utf-8");         
		      response.setContentType("text/html; charset=utf-8");
		      
		      PrintWriter out=response.getWriter();
		      out.println(str);
		      out.flush();
		      out.close();
				
		  }catch(Exception e){
			  e.printStackTrace();
		  }
		return null;
	}

合成gif用到的java类 下载即可用   

http://download.csdn.net/detail/liyanhui1001/6225079

抱歉!评论已关闭.