浏览 1980 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-03-09
最后修改:2011-03-09
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; } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-03-10
最后修改:2011-03-10
创新科技,来自JE代码....
|
|
返回顶楼 | |