/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.yuntong.action.ajaxaction;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/** 
 * MyEclipse Struts
 * Creation date: 04-13-2007
 * 
 * XDoclet definition:
 * @struts.action path="/g_UpLoadPicture" name="g_UpLoadPictureForm" scope="request" validate="true"
 */
public class G_UpLoadPictureAction extends Action {
	/*
	 * Generated Methods
	 */

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws IOException 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		String type=request.getParameter("type");
		String path=new String(request.getParameter("path").getBytes("ISO-8859-1"),"UTF-8");
		String files=path.replace("\\","/");
		String fileName=files.split("/")[files.split("/").length-1];
		File file=new File(path);
		Image src = javax.imageio.ImageIO.read(file);
		int wideth=src.getWidth(null); //得到源图宽
		int height=src.getHeight(null); //得到源图长
		BufferedImage tag = new BufferedImage(160,120,BufferedImage.TYPE_INT_RGB);
		tag.getGraphics().drawImage(src,0,0,160,120,null); //绘制图
		String str=request.getSession().getServletContext().getRealPath("/");
		String savePath="";
		if(type.equals("car")){  //判断类型,保存在不同的文件夹下
			savePath=str+"picture\\car\\"+fileName;
		}
		if(type.equals("driver")){
			savePath=str+"picture\\driver\\"+fileName;
		}
		if(!new File(str+"picture\\car\\").isDirectory()){  //如果不存在文件夹,就创建一个
			new File(str+"picture\\car\\").mkdirs();
		}
		if(!new File(str+"picture\\driver\\").isDirectory()){  //如果不存在文件夹,就创建一个
			new File(str+"picture\\driver\\").mkdirs();
		}
		FileOutputStream out=new FileOutputStream(savePath); //输出到文件流
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
		encoder.encode(tag); //近JPEG编码
		out.close();
		return null;
	}
}