全自动用kindeditor文本编辑器实现后台产品描述图片的增删改(投点分给 thank you)

    详细分析在代码上有。。有疑问、问题的可以跟贴问。。。。

public class productaction extends baseaction {		// 添加商品	public string addproduct() throws exception {		// todo auto-generated method stub		product p = new product(name.trim(), brane, price, marketprice, weight,				size, style00, material, colordesc, situation, big, style01,				logo, age, struct, element, desc, total);				this.adminservice.saveorupdate(p);		file dir = new file(servletactioncontext.getrequest().getrealpath(				"/productimg"));		if (!dir.exists()) {			dir.mkdir();		}		file dst = null;		boolean b = false;		// 形象照		if (img != null) {			dst = new file(dir.getabsolutepath() + file.separator + p.getid()					+ ".jpg");			// 图像缩咯			if ("0".equals(this.model)) {				b = this.resize_image(img, dst, 178, 178);			} else {				b = this.resize_image(img, dst, 180, 300);			}			if (b)				p.setimg(dst.getname());		}		// 产口图片		string temp = "";		if (productimg != null) {			for (int i = 0; i < productimg.length; i++) {				// 图像压缩				if (productimg[i] != null) {					dst = new file(dir.getabsolutepath() + file.separator							+ "p_" + p.getid() + new date().gettime() + i							+ ".jpg");					// 原图					this.resize_image(productimg[i], dst);					temp += dst.getname() + "-";					// 小图 用s_标记 在原图的文件名上加上s					dst = new file(dir.getabsolutepath() + file.separator							+ "s_" + dst.getname());					this.geometric_image(productimg[i], dst, 80, 80);				}			}			p.setproductimg(temp);		}		// 颜色图片		temp = "";		if (colorimg != null) {			for (int i = 0; i < colorimg.length; i++) {				// 图像压缩				if (colorimg[i] != null) {					dst = new file(dir.getabsolutepath() + file.separator							+ "c_" + p.getid() + new date().gettime() + i							+ ".jpg");					// 原图					this.resize_image(colorimg[i], dst);					temp += dst.getname() + "-";					// 小图					dst = new file(dir.getabsolutepath() + file.separator							+ "s_" + dst.getname());					this.geometric_image(colorimg[i], dst, 80, 80);				}			}			p.setcolorimg(temp);		}		this.adminservice.saveorupdate(p);		this.adminservice				.jsp_to_html("http://www.xinbw.com/toproduct.action?id="						+ p.getid(), "producthtml" + file.separator + "pshow_"						+ p.getid() + ".html", servletactioncontext						.getrequest());//删除产品描述图片		if (!this.isnull(imgstr)) {			if (p.getdesc() == null) {				p.setdesc("");			}			string arr[] = imgstr.split("-");			for (int i = 0; i < arr.length; i++) {				if (p.getdesc().indexof(arr[i]) == -1) {					// 删除图片					file ft = new file(							servletactioncontext.getrequest().getrealpath(									"descimg" + arr[i].split("descimg")[1]));					if (ft.exists()) {						ft.delete();						// system.out.println("-delete-->" + ft.getname());					}				}			}		}	// system.out.println("-->" + model);		this.msg = "商品添加成功!";		return super.execute();	}		// 更新商品	public string updateproduct() throws exception {		// todo auto-generated method stub		product = (product) this.adminservice.get(product.class, id);		//原来的商品描述图片		string text = product.getdesc();		list list = new arraylist();		if (text != null) {			string temp[] = text.split("descimg/");			for (int k = 0; k < temp.length; k++) {				if (temp[k].indexof("jpg") != -1) {					// system.out.println("---->"+temp[k]);					list.add(temp[k].substring(0, temp[k].indexof("jpg"))							+ "jpg");					// system.out.println("---->"+temp[k]+".jpg");				}			}		}		this.adminservice.saveorupdate(product);		file dir = new file(servletactioncontext.getrequest().getrealpath(				"/productimg"));		if (!dir.exists()) {			dir.mkdir();		}		file dst = null;		boolean b = false;		// 形象照		if (img != null) {			dst = new file(dir.getabsolutepath() + file.separator					+ product.getid() + ".jpg");			// 图像缩咯			// 图像缩咯			if ("0".equals(this.model)) {				b = this.resize_image(img, dst, 178, 178);			} else {				b = this.resize_image(img, dst, 180, 300);			}			if (b)				product.setimg(dst.getname());		}		// 产口图片		string temp = "";		if (productimg != null) {			for (int i = 0; i < productimg.length; i++) {				// 图像压缩				if (productimg[i] != null) {					dst = new file(dir.getabsolutepath() + file.separator							+ "p_" + product.getid() + new date().gettime() + i							+ ".jpg");					// 原图					this.resize_image(productimg[i], dst);					temp += dst.getname() + "-";					// 小图 用s_标记					dst = new file(dir.getabsolutepath() + file.separator							+ "s_" + dst.getname());					this.geometric_image(productimg[i], dst, 80, 80);				}			}			product.setproductimg(product.getproductimg() + temp);		}		// 颜色图片		temp = "";		if (colorimg != null) {			for (int i = 0; i < colorimg.length; i++) {				// 图像压缩				if (colorimg[i] != null) {					dst = new file(dir.getabsolutepath() + file.separator							+ "c_" + product.getid() + new date().gettime() + i							+ ".jpg");					// 原图					this.resize_image(colorimg[i], dst);					temp += dst.getname() + "-";					// 小图					dst = new file(dir.getabsolutepath() + file.separator							+ "s_" + dst.getname());					this.geometric_image(colorimg[i], dst, 80, 80);				}			}			product.setcolorimg(product.getcolorimg() + temp);		}		// system.out.println("-->"+product.getproductimg()+"---->"+product.getcolorimg());		this.adminservice.saveorupdate(product);		this.adminservice.jsp_to_html(				"http://www.xinbw.com/toproduct.action?id=" + product.getid(),				"producthtml/pshow_" + product.getid() + ".html",				servletactioncontext.getrequest());		if (!this.isnull(imgstr)) {			if (product.getdesc() == null) {				product.setdesc("");			}			string arr[] = imgstr.split("-");			for (int i = 0; i < arr.length; i++) {				if (product.getdesc().indexof(arr[i]) == -1) {					// 删除图片					file ft = new file(							servletactioncontext.getrequest().getrealpath(									"descimg" + arr[i].split("descimg")[1]));					if (ft.exists()) {						ft.delete();					}				}			}		}		//新的商品描述图片		text = product.getdesc();		list<string> list_ = new arraylist();		if (text != null) {			string str[] = text.split("descimg/");			for (int k = 0; k < str.length; k++) {				if (str[k].indexof("jpg") != -1) {					// system.out.println("---->"+str[k]);					list_.add(str[k].substring(0, str[k].indexof("jpg"))							+ "jpg");					// system.out.println("---->"+temp[k]+".jpg");				}			}		}		// 一比较删除相应的图片//删除产品描述图片		for (int i = 0; i < list.size(); i++) {			string jpg = (string) list.get(i);			if (!list_.contains(jpg)) {				// 删除图片				file ft = new file(servletactioncontext.getrequest()						.getrealpath("descimg/" + jpg));				if (ft.exists()) {					ft.delete();					// system.out.println("------------>"+ft.getname());				}			}		}		this.date = new date().gettime();		this.msg = "商品修改成功!";		return "updateproduct";	}		/**	 * 图像压缩	 * 	 * @param fi大图文件	 * @param fo将要转换出的小图文件	 */	public boolean resize_image(file fi, file fo) {		try {			// file fi = new file("e:/3.jpg"); // 大图文件			// file fo = new file("e:/333.jpg"); // 将要转换出的小图文件			// affinetransform transform = new affinetransform();			bufferedimage bis = imageio.read(fi);			int w = bis.getwidth();			// system.out.println(w);			int h = bis.getheight();			// system.out.println(h);			// double scale = (double) w / h;			int nw = w;			int nh = h;			bufferedimage tag = new bufferedimage(nw, nh,					bufferedimage.type_int_rgb);			tag.getgraphics().drawimage(bis, 0, 0, nw, nh, null); // 绘制缩小后的图			// 转换为32*32 jpg格式			fileoutputstream newimage = new fileoutputstream(fo); // 输出到文件流			jpegimageencoder encoder = jpegcodec.createjpegencoder(newimage);			encoder.encode(tag);			newimage.close();			return true;		} catch (exception e) {			e.printstacktrace();			return false;		}	}	/**	 * 图像的缩略	 * 	 * 	 * width 输出图片大小 height	 * 	 * @param fi大图文件	 * @param fo将要转换出的小图文件	 */	public boolean resize_image(file fi, file fo, int width, int height) {		try {			bufferedimage bis = imageio.read(fi);			int w = bis.getwidth();			int h = bis.getheight();			int nw = width;			int nh = height;			bufferedimage tag = new bufferedimage(nw, nh,					bufferedimage.type_int_rgb);			tag.getgraphics().drawimage(bis, 0, 0, nw, nh, null); // 绘制缩小后的图			// 转换为32*32 jpg格式			fileoutputstream newimage = new fileoutputstream(fo); // 输出到文件流			jpegimageencoder encoder = jpegcodec.createjpegencoder(newimage);			encoder.encode(tag);			newimage.close();			return true;		} catch (exception e) {			e.printstacktrace();			return false;		}	}	/**	 * 图像的等比缩略	 * 	 * 	 * width 输出图片大小 height	 * 	 * @param fi大图文件	 * @param fo将要转换出的小图文件	 */	public boolean geometric_image(file fi, file fo, int width, int height) {		try {			bufferedimage bis = imageio.read(fi);			int w = bis.getwidth();			int h = bis.getheight();			int nw = w;			int nh = h;			if (w > width) {				nw = width;				nh = nw * h / w;				if (nh > height) {					nh = height;					nw = nh * w / h;				}			} else if (h > height) {				nh = height;				nw = nh * w / h;				if (nw > width) {					nw = width;					nh = nw * h / w;				}			}			bufferedimage tag = new bufferedimage(nw, nh,					bufferedimage.type_int_rgb);			tag.getgraphics().drawimage(bis, 0, 0, nw, nh, null); // 绘制缩小后的图			// 转换为32*32 jpg格式			fileoutputstream newimage = new fileoutputstream(fo); // 输出到文件流			jpegimageencoder encoder = jpegcodec.createjpegencoder(newimage);			encoder.encode(tag);			newimage.close();			return true;		} catch (exception e) {			e.printstacktrace();			return false;		}	}}
 

你可能感兴趣的:(java,工作)