superyang 发表于 2013-2-7 20:12:00

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

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

public class ProductAction extends BaseAction {// 添加商品public String addproduct() throws Exception {// TODO Auto-generated method stubProduct 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 != null) {dst = new File(dir.getAbsolutePath() + File.separator+ "p_" + p.getId() + new Date().getTime() + i+ ".jpg");// 原图this.resize_image(productimg, dst);temp += dst.getName() + "-";// 小图 用s_标记 在原图的文件名上加上sdst = new File(dir.getAbsolutePath() + File.separator+ "s_" + dst.getName());this.geometric_image(productimg, dst, 80, 80);}}p.setProductimg(temp);}// 颜色图片temp = "";if (colorimg != null) {for (int i = 0; i < colorimg.length; i++) {// 图像压缩if (colorimg != null) {dst = new File(dir.getAbsolutePath() + File.separator+ "c_" + p.getId() + new Date().getTime() + i+ ".jpg");// 原图this.resize_image(colorimg, dst);temp += dst.getName() + "-";// 小图dst = new File(dir.getAbsolutePath() + File.separator+ "s_" + dst.getName());this.geometric_image(colorimg, 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) == -1) {// 删除图片File ft = new File(ServletActionContext.getRequest().getRealPath("descimg" + arr.split("descimg")));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 stubproduct = (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.indexOf("jpg") != -1) {// System.out.println("---->"+temp);list.add(temp.substring(0, temp.indexOf("jpg"))+ "jpg");// System.out.println("---->"+temp+".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 != null) {dst = new File(dir.getAbsolutePath() + File.separator+ "p_" + product.getId() + new Date().getTime() + i+ ".jpg");// 原图this.resize_image(productimg, dst);temp += dst.getName() + "-";// 小图 用s_标记dst = new File(dir.getAbsolutePath() + File.separator+ "s_" + dst.getName());this.geometric_image(productimg, dst, 80, 80);}}product.setProductimg(product.getProductimg() + temp);}// 颜色图片temp = "";if (colorimg != null) {for (int i = 0; i < colorimg.length; i++) {// 图像压缩if (colorimg != null) {dst = new File(dir.getAbsolutePath() + File.separator+ "c_" + product.getId() + new Date().getTime() + i+ ".jpg");// 原图this.resize_image(colorimg, dst);temp += dst.getName() + "-";// 小图dst = new File(dir.getAbsolutePath() + File.separator+ "s_" + dst.getName());this.geometric_image(colorimg, 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) == -1) {// 删除图片File ft = new File(ServletActionContext.getRequest().getRealPath("descimg" + arr.split("descimg")));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.indexOf("jpg") != -1) {// System.out.println("---->"+str);list_.add(str.substring(0, str.indexOf("jpg"))+ "jpg");// System.out.println("---->"+temp+".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;}}}
页: [1]
查看完整版本: 全自动用kindeditor文本编辑器实现后台产品描述图片的增删改(投点分给 thank you)