struts2 或 spring mvc上传图片生成高清缩略图

来源:互联网 发布:网络电视盒子品牌好 编辑:程序博客网 时间:2024/04/28 15:04

以下是struts2也有上传图片哦。 如果要看 spring mvc上传图片 请直接拉下下

第一种:清晰度不高;

UploadAction

 

[java] view plaincopyprint?
  1. package com.lanyuan.upload;  
  2.   
  3. import java.io.File;  
  4. import com.opensymphony.xwork2.ActionSupport;  
  5.   
  6. public class UploadAction extends ActionSupport {  
  7.     private static final long serialVersionUID = -8204063374280945416L;  
  8.     private File upload;// 路径  
  9.     private String uploadFileName;// 原文件名  
  10.     private String uploadContentType;// 文件类型  
  11.     public String upload() throws Exception {  
  12.         UploadUtil uploadutil = new UploadUtil();  
  13.         uploadutil.uploadImage1(getUpload(), getUploadContentType(), getUploadFileName());  
  14.         return "success";  
  15.     }  
  16.     public File getUpload() {  
  17.         return upload;  
  18.     }  
  19.     public void setUpload(File upload) {  
  20.         this.upload = upload;  
  21.     }  
  22.     public String getUploadFileName() {  
  23.         return uploadFileName;  
  24.     }  
  25.     public void setUploadFileName(String uploadFileName) {  
  26.         this.uploadFileName = uploadFileName;  
  27.     }  
  28.     public String getUploadContentType() {  
  29.         return uploadContentType;  
  30.     }  
  31.     public void setUploadContentType(String uploadContentType) {  
  32.         this.uploadContentType = uploadContentType;  
  33.     }  
  34. }  

 

/**
 *
 * 上传图片 工具类
 * 大图片路径,生成小图片路径,
 * 大图片文件名,生成小图片文名,
 * 生成小图片宽度,生成小图片高度,
 * 是否等比缩放(默认为true))
 *
 * @author Administrator
 *
 */

UploadUtil

[java] view plaincopyprint?
  1. package com.lanyuan.upload;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.text.DateFormat;  
  8. import java.text.SimpleDateFormat;  
  9. import java.util.Date;  
  10. import java.util.Random;  
  11.   
  12. import org.apache.struts2.ServletActionContext;  
  13. /** 
  14.  *  
  15.  * 上传图片 工具类 
  16.  * 大图片路径,生成小图片路径, 
  17.  * 大图片文件名,生成小图片文名, 
  18.  * 生成小图片宽度,生成小图片高度, 
  19.  * 是否等比缩放(默认为true)) 
  20.  *  
  21.  * @author Administrator 
  22.  *  
  23.  */  
  24. public class UploadUtil {  
  25.     private String imagePath = "/image/"+new SimpleDateFormat("yyyyMMddHH").format(new Date())+"";// 配置图片路径  
  26.     private String image_smallPath = "/image_small/"+new SimpleDateFormat("yyyyMMddHH").format(new Date())+"";// 配置小图片路径  
  27.       
  28.     /** 
  29.      *  
  30.      * @param request /  
  31.      * @param getUpload  路径 
  32.      * @param getUploadContentType  文件类型 
  33.      * @param getUploadFileName 原文件名 
  34.      * @return 
  35.      * @throws IOException 
  36.      */  
  37.     public void uploadImage1(File getUpload, String getUploadContentType, String getUploadFileName) throws IOException {  
  38.   
  39.         String getImagePath = ServletActionContext.getServletContext().getRealPath(imagePath);  
  40.   
  41.         String getImage_smallPath = ServletActionContext.getServletContext().getRealPath(image_smallPath);  
  42.         File image = new File(getImagePath);  
  43.         if (!image.exists()) {  
  44.             image.mkdir();  
  45.         }  
  46.         File image_small = new File(getImage_smallPath);  
  47.         if (!image_small.exists()) {  
  48.             image_small.mkdir();  
  49.         }  
  50.           
  51.         // 得到上传文件的后缀名  
  52.         String uploadName = getUploadContentType;  
  53.         System.out.println("图片类型 ------------"+uploadName);  
  54.   
  55.         String lastuploadName = uploadName.substring(  
  56.                 uploadName.indexOf("/") + 1, uploadName.length());  
  57.         System.out.println("得到上传文件的后缀名 ------------"+lastuploadName);  
  58.           
  59.         // 得到文件的新名字  
  60.         String fileNewName = generateFileName(getUploadFileName);  
  61.         System.out.println("// 得到文件的新名字 ------------"+fileNewName);  
  62.           
  63.         FileOutputStream fos = new FileOutputStream(getImagePath + "/" + fileNewName);  
  64.           
  65.         FileInputStream fis = new FileInputStream(getUpload);  
  66.         byte[] buffer = new byte[1024];  
  67.         int len = 0;  
  68.         while ((len = fis.read(buffer)) > 0) {  
  69.             fos.write(buffer, 0, len);  
  70.         }  
  71.         DwindlePic mypic = new DwindlePic();  
  72.         mypic.s_pic(getImagePath + "/", getImage_smallPath + "/", fileNewName, fileNewName, 110110false);  
  73.           
  74.         //最后返回图片路径  
  75.         imagePath = imagePath+"/"+fileNewName;  
  76.         image_smallPath = image_smallPath+"/"+fileNewName;  
  77.     }  
  78.   
  79.       
  80.     /** 
  81.      * 传入原图名称,,获得一个以时间格式的新名称 
  82.      * @param fileName 原图名称 
  83.      * @return 
  84.      */  
  85.     private String generateFileName(String fileName) {  
  86.         DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");  
  87.         String formatDate = format.format(new Date());  
  88.         int random = new Random().nextInt(10000);  
  89.         int position = fileName.lastIndexOf(".");  
  90.         String extension = fileName.substring(position);  
  91.         return formatDate + random + extension;  
  92.     }  
  93.   
  94.   
  95.     public  String getImagepath() {  
  96.         return imagePath;  
  97.     }  
  98.   
  99.   
  100.     public  String getImageSmallpath() {  
  101.         return image_smallPath;  
  102.     }  
  103. }  


大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))

DwindlePic

 

[java] view plaincopyprint?
  1. package com.lanyuan.upload;  
  2.   
  3. import java.io.*;  
  4.   
  5. import com.sun.image.codec.jpeg.*;  
  6. import java.awt.image.*;  
  7. import java.awt.*;  
  8. import java.applet.*;  
  9.   
  10. /** 
  11.  * s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true)) 
  12.  *  
  13.  * @author Administrator 
  14.  *  
  15.  */  
  16. public class DwindlePic {  
  17.     String InputDir; // 输入图路径  
  18.     String OutputDir; // 输出图路径  
  19.     String InputFileName; // 输入图文件名  
  20.     String OutputFileName; // 输出图文件名  
  21.     int OutputWidth = 110// 默认输出图片宽  
  22.     int OutputHeight = 110// 默认输出图片高  
  23.     int rate = 0;  
  24.     boolean proportion = true// 是否等比缩放标记(默认为等比缩放)  
  25.   
  26.     public DwindlePic() {  
  27.         // 初始化变量  
  28.         InputDir = "";  
  29.         OutputDir = "";  
  30.         InputFileName = "";  
  31.         OutputFileName = "";  
  32.         OutputWidth = 110;  
  33.         OutputHeight = 110;  
  34.         rate = 0;  
  35.     }  
  36.   
  37.     public boolean s_pic() {  
  38.         // BufferedImage image;  
  39.         // String NewFileName;  
  40.         // 建立输出文件对象  
  41.         File file = new File(OutputDir + OutputFileName);  
  42.         FileOutputStream tempout = null;  
  43.         try {  
  44.             tempout = new FileOutputStream(file);  
  45.         } catch (Exception ex) {  
  46.             System.out.println(ex.toString());  
  47.         }  
  48.         Image img = null;  
  49.         Toolkit tk = Toolkit.getDefaultToolkit();  
  50.         Applet app = new Applet();  
  51.         MediaTracker mt = new MediaTracker(app);  
  52.         try {  
  53.             img = tk.getImage(InputDir + InputFileName);  
  54.             mt.addImage(img, 0);  
  55.             mt.waitForID(0);  
  56.         } catch (Exception e) {  
  57.             e.printStackTrace();  
  58.         }  
  59.         if (img.getWidth(null) == -1) {  
  60.             System.out.println(" can't read,retry!" + "<BR>");  
  61.             return false;  
  62.         } else {  
  63.             int new_w;  
  64.             int new_h;  
  65.             if (this.proportion == true) { // 判断是否是等比缩放.  
  66.                 // 为等比缩放计算输出的图片宽度及高度  
  67.                 double rate1 = ((double) img.getWidth(null))  
  68.                         / (double) OutputWidth + 0.1;  
  69.                 double rate2 = ((double) img.getHeight(null))  
  70.                         / (double) OutputHeight + 0.1;  
  71.                 double rate = rate1 > rate2 ? rate1 : rate2;  
  72.                 new_w = (int) (((double) img.getWidth(null)) / rate);  
  73.                 new_h = (int) (((double) img.getHeight(null)) / rate);  
  74.             } else {  
  75.                 new_w = OutputWidth; // 输出的图片宽度  
  76.                 new_h = OutputHeight; // 输出的图片高度  
  77.             }  
  78.             BufferedImage buffImg = new BufferedImage(new_w, new_h,  
  79.                     BufferedImage.TYPE_INT_RGB);  
  80.             Graphics g = buffImg.createGraphics();  
  81.             g.setColor(Color.white);  
  82.             g.fillRect(00, new_w, new_h);  
  83.             g.drawImage(img, 00, new_w, new_h, null);  
  84.             g.dispose();  
  85.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);  
  86.             try {  
  87.                 encoder.encode(buffImg);  
  88.                 tempout.close();  
  89.             } catch (IOException ex) {  
  90.                 System.out.println(ex.toString());  
  91.             }  
  92.         }  
  93.         return true;  
  94.     }  
  95.   
  96.     public boolean s_pic(String InputDir, String OutputDir,  
  97.             String InputFileName, String OutputFileName) {  
  98.         // 输入图路径  
  99.         this.InputDir = InputDir;  
  100.         // 输出图路径  
  101.         this.OutputDir = OutputDir;  
  102.         // 输入图文件名  
  103.         this.InputFileName = InputFileName;  
  104.         // 输出图文件名  
  105.         this.OutputFileName = OutputFileName;  
  106.         return s_pic();  
  107.     }  
  108.       
  109.        
  110.       /** 
  111.        * s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true)) 
  112.        * @param InputDir 
  113.        * @param OutputDir 
  114.        * @param InputFileName 
  115.        * @param OutputFileName 
  116.        * @param width 
  117.        * @param height 
  118.        * @param gp 
  119.        * @return 
  120.        */  
  121.       
  122.     public boolean s_pic(String InputDir, String OutputDir,  
  123.             String InputFileName, String OutputFileName, int width, int height,  
  124.             boolean gp) {  
  125.         // 输入图路径  
  126.         this.InputDir = InputDir;  
  127.         // 输出图路径  
  128.         this.OutputDir = OutputDir;  
  129.         // 输入图文件名  
  130.         this.InputFileName = InputFileName;  
  131.         // 输出图文件名  
  132.         this.OutputFileName = OutputFileName;  
  133.         // 设置图片长宽  
  134.         setW_H(width, height);  
  135.         // 是否是等比缩放 标记  
  136.         this.proportion = gp;  
  137.         return s_pic();  
  138.     }  
  139.   
  140.     public void setInputDir(String InputDir) {  
  141.         this.InputDir = InputDir;  
  142.     }  
  143.   
  144.     public void setOutputDir(String OutputDir) {  
  145.         this.OutputDir = OutputDir;  
  146.     }  
  147.   
  148.     public void setInputFileName(String InputFileName) {  
  149.         this.InputFileName = InputFileName;  
  150.     }  
  151.   
  152.     public void setOutputFileName(String OutputFileName) {  
  153.         this.OutputFileName = OutputFileName;  
  154.     }  
  155.   
  156.     public void setOutputWidth(int OutputWidth) {  
  157.         this.OutputWidth = OutputWidth;  
  158.     }  
  159.   
  160.     public void setOutputHeight(int OutputHeight) {  
  161.         this.OutputHeight = OutputHeight;  
  162.     }  
  163.   
  164.     public void setW_H(int width, int height) {  
  165.         this.OutputWidth = width;  
  166.         this.OutputHeight = height;  
  167.     }  
  168. }  


看看效果吧!

 

第二种,清晰度还可以接受

 

[java] view plaincopyprint?
  1. package com.xiyuan.upload;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.text.DateFormat;  
  9. import java.text.SimpleDateFormat;  
  10. import java.util.Date;  
  11. import java.util.Random;  
  12.   
  13. import javax.imageio.ImageIO;  
  14.   
  15. import org.apache.struts2.ServletActionContext;  
  16.   
  17.   
  18. /** 
  19.  *  
  20.  * 上传图片 工具类 大图片路径,生成小图片路径, 大图片文件名,生成小图片文名, 生成小图片宽度,生成小图片高度, 是否等比缩放(默认为true)) 
  21.  *  
  22.  * @author Administrator 
  23.  *  
  24.  */  
  25. public class UploadUtil  
  26. {  
  27.     private String imagePath = "/imageFile/" + new SimpleDateFormat("yyyyMMddHH").format(new Date()) + "";// 配置图片路径  
  28.   
  29.     /** 
  30.      *  
  31.      * @param getUpload 
  32.      *            路径 
  33.      * @param getUploadContentType 
  34.      *            文件类型 
  35.      * @param getUploadFileName 
  36.      *            原文件名 
  37.      * @return 
  38.      * @throws IOException 
  39.      */  
  40.     public void uploadImage1(File getUpload, String getUploadContentType, String getUploadFileName) throws IOException  
  41.     {  
  42.   
  43.         String getImagePath = ServletActionContext.getServletContext().getRealPath(imagePath);  
  44.   
  45.         File image = new File(getImagePath);  
  46.         if (!image.exists())  
  47.         {  
  48.             image.mkdir();  
  49.         }  
  50.   
  51.         // 得到上传文件的后缀名  
  52.         String uploadName = getUploadContentType;  
  53.         System.out.println("图片类型 ------------" + uploadName);  
  54.   
  55.         String lastuploadName = uploadName.substring(uploadName.indexOf("/") + 1, uploadName.length());  
  56.         System.out.println("得到上传文件的后缀名 ------------" + lastuploadName);  
  57.   
  58.         // 得到文件的新名字  
  59.         String fileNewName = generateFileName(getUploadFileName);  
  60.         System.out.println("// 得到文件的新名字 ------------" + fileNewName);  
  61.   
  62.         // FileOutputStream fos = new FileOutputStream(getImagePath + "/" +  
  63.         // fileNewName);  
  64.         //        
  65.         // FileInputStream fis = new FileInputStream(getUpload);  
  66.         // byte[] buffer = new byte[1024];  
  67.         // int len = 0;  
  68.         // while ((len = fis.read(buffer)) > 0) {  
  69.         // fos.write(buffer, 0, len);  
  70.         // }  
  71.   
  72.         // 最后返回图片路径  
  73.         imagePath = imagePath + "/" + fileNewName;  
  74.         System.out.println(" 回图片路径   " + getUpload);  
  75.         System.out.println("        //最后返回图片路径   " + imagePath);  
  76.   
  77.         BufferedImage srcBufferImage = ImageIO.read(getUpload);  
  78.         System.out.println(" w " + srcBufferImage.getWidth() + " w " + srcBufferImage.getHeight());  
  79.         BufferedImage scaledImage;  
  80.         ScaleImage scaleImage = ScaleImage.getInstance();  
  81.         int yw = srcBufferImage.getWidth();  
  82.         int yh = srcBufferImage.getHeight();  
  83.         int w = 110, h = 110;  
  84.         // 如果上传图片 宽高 比 压缩的要小 则不压缩  
  85.         if (w > yw && h > yh)  
  86.         {  
  87.             FileOutputStream fos = new FileOutputStream(getImagePath + "/" + fileNewName);  
  88.   
  89.             FileInputStream fis = new FileInputStream(getUpload);  
  90.             byte[] buffer = new byte[1024];  
  91.             int len = 0;  
  92.             while ((len = fis.read(buffer)) > 0)  
  93.             {  
  94.                 fos.write(buffer, 0, len);  
  95.             }  
  96.         }  
  97.         else  
  98.         {  
  99.             scaledImage = scaleImage.imageZoomOut(srcBufferImage, w, h);  
  100.             FileOutputStream out = new FileOutputStream(getImagePath + "/" + fileNewName);  
  101.             ImageIO.write(scaledImage, "jpeg", out);  
  102.   
  103.         }  
  104.     }  
  105.   
  106.     /** 
  107.      * 传入原图名称,,获得一个以时间格式的新名称 
  108.      *  
  109.      * @param fileName 
  110.      *             原图名称 
  111.      * @return 
  112.      */  
  113.     private String generateFileName(String fileName)  
  114.     {  
  115.         DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");  
  116.         String formatDate = format.format(new Date());  
  117.         int random = new Random().nextInt(10000);  
  118.         int position = fileName.lastIndexOf(".");  
  119.         String extension = fileName.substring(position);  
  120.         return formatDate + random + extension;  
  121.     }  
  122.   
  123.     public String getImagepath()  
  124.     {  
  125.         return imagePath;  
  126.     }  
  127.   
  128. }  


 

ScaleImage

 

[java] view plaincopyprint?
  1. package com.xiyuan.upload;  
  2.   
  3.   
  4. import java.awt.image.BufferedImage;  
  5.   
  6. public class ScaleImage {  
  7.     private int width;  
  8.   
  9.     private int height;  
  10.   
  11.     private int scaleWidth;  
  12.   
  13.     private double support = (double3.0;  
  14.   
  15.     private double PI = (double3.14159265358978;  
  16.   
  17.     private double[] contrib;  
  18.   
  19.     private double[] normContrib;  
  20.   
  21.     private double[] tmpContrib;  
  22.   
  23.     private int nDots;  
  24.   
  25.     private int nHalfDots;  
  26.   
  27.     /** 
  28.      * Start: Use Lanczos filter to replace the original algorithm for image 
  29.      * scaling. Lanczos improves quality of the scaled image modify by :blade 
  30.      */  
  31.     private static ScaleImage instance = new ScaleImage();  
  32.     private ScaleImage(){};  
  33.     public static ScaleImage getInstance(){  
  34.         return instance;  
  35.     }  
  36.     public BufferedImage imageZoomOut(BufferedImage srcBufferImage, int w, int h) {  
  37.         width = srcBufferImage.getWidth();  
  38.         height = srcBufferImage.getHeight();  
  39.         scaleWidth = w;  
  40.   
  41.         if (DetermineResultSize(w, h) == 1) {  
  42.             return srcBufferImage;  
  43.         }  
  44.         CalContrib();  
  45.         BufferedImage pbOut = HorizontalFiltering(srcBufferImage, w);  
  46.         BufferedImage pbFinalOut = VerticalFiltering(pbOut, h);  
  47.         return pbFinalOut;  
  48.     }  
  49.   
  50.     /** 
  51.      * 决定图像尺寸 
  52.      */  
  53.     private int DetermineResultSize(int w, int h) {  
  54.         double scaleH, scaleV;  
  55.         scaleH = (double) w / (double) width;  
  56.         scaleV = (double) h / (double) height;  
  57.         // �?��判断�?��scaleH,scaleV,不做放大操�?  
  58.         if (scaleH >= 1.0 && scaleV >= 1.0) {  
  59.             return 1;  
  60.         }  
  61.         return 0;  
  62.   
  63.     } // end of DetermineResultSize()  
  64.   
  65.     private double Lanczos(int i, int inWidth, int outWidth, double Support) {  
  66.         double x;  
  67.   
  68.         x = (double) i * (double) outWidth / (double) inWidth;  
  69.   
  70.         return Math.sin(x * PI) / (x * PI) * Math.sin(x * PI / Support)  
  71.                 / (x * PI / Support);  
  72.   
  73.     } // end of Lanczos()  
  74.   
  75.     //    
  76.     // Assumption: same horizontal and vertical scaling factor  
  77.     //    
  78.     private void CalContrib() {  
  79.         nHalfDots = (int) ((double) width * support / (double) scaleWidth);  
  80.         nDots = nHalfDots * 2 + 1;  
  81.         try {  
  82.             contrib = new double[nDots];  
  83.             normContrib = new double[nDots];  
  84.             tmpContrib = new double[nDots];  
  85.         } catch (Exception e) {  
  86.             System.out.println("init   contrib,normContrib,tmpContrib" + e);  
  87.         }  
  88.   
  89.         int center = nHalfDots;  
  90.         contrib[center] = 1.0;  
  91.   
  92.         double weight = 0.0;  
  93.         int i = 0;  
  94.         for (i = 1; i <= center; i++) {  
  95.             contrib[center + i] = Lanczos(i, width, scaleWidth, support);  
  96.             weight += contrib[center + i];  
  97.         }  
  98.   
  99.         for (i = center - 1; i >= 0; i--) {  
  100.             contrib[i] = contrib[center * 2 - i];  
  101.         }  
  102.   
  103.         weight = weight * 2 + 1.0;  
  104.   
  105.         for (i = 0; i <= center; i++) {  
  106.             normContrib[i] = contrib[i] / weight;  
  107.         }  
  108.   
  109.         for (i = center + 1; i < nDots; i++) {  
  110.             normContrib[i] = normContrib[center * 2 - i];  
  111.         }  
  112.     } // end of CalContrib()  
  113.   
  114.     // 处理边缘  
  115.     private void CalTempContrib(int start, int stop) {  
  116.         double weight = 0;  
  117.   
  118.         int i = 0;  
  119.         for (i = start; i <= stop; i++) {  
  120.             weight += contrib[i];  
  121.         }  
  122.   
  123.         for (i = start; i <= stop; i++) {  
  124.             tmpContrib[i] = contrib[i] / weight;  
  125.         }  
  126.   
  127.     } // end of CalTempContrib()  
  128.   
  129.     private int GetRedValue(int rgbValue) {  
  130.         int temp = rgbValue & 0x00ff0000;  
  131.         return temp >> 16;  
  132.     }  
  133.   
  134.     private int GetGreenValue(int rgbValue) {  
  135.         int temp = rgbValue & 0x0000ff00;  
  136.         return temp >> 8;  
  137.     }  
  138.   
  139.     private int GetBlueValue(int rgbValue) {  
  140.         return rgbValue & 0x000000ff;  
  141.     }  
  142.   
  143.     private int ComRGB(int redValue, int greenValue, int blueValue) {  
  144.   
  145.         return (redValue << 16) + (greenValue << 8) + blueValue;  
  146.     }  
  147.   
  148.     // 行水平滤�?  
  149.     private int HorizontalFilter(BufferedImage bufImg, int startX, int stopX,  
  150.             int start, int stop, int y, double[] pContrib) {  
  151.         double valueRed = 0.0;  
  152.         double valueGreen = 0.0;  
  153.         double valueBlue = 0.0;  
  154.         int valueRGB = 0;  
  155.         int i, j;  
  156.   
  157.         for (i = startX, j = start; i <= stopX; i++, j++) {  
  158.             valueRGB = bufImg.getRGB(i, y);  
  159.   
  160.             valueRed += GetRedValue(valueRGB) * pContrib[j];  
  161.             valueGreen += GetGreenValue(valueRGB) * pContrib[j];  
  162.             valueBlue += GetBlueValue(valueRGB) * pContrib[j];  
  163.         }  
  164.   
  165.         valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen),  
  166.                 Clip((int) valueBlue));  
  167.         return valueRGB;  
  168.   
  169.     } // end of HorizontalFilter()  
  170.   
  171.     // 图片水平滤波  
  172.     private BufferedImage HorizontalFiltering(BufferedImage bufImage, int iOutW) {  
  173.         int dwInW = bufImage.getWidth();  
  174.         int dwInH = bufImage.getHeight();  
  175.         int value = 0;  
  176.         BufferedImage pbOut = new BufferedImage(iOutW, dwInH,  
  177.                 BufferedImage.TYPE_INT_RGB);  
  178.   
  179.         for (int x = 0; x < iOutW; x++) {  
  180.   
  181.             int startX;  
  182.             int start;  
  183.             int X = (int) (((double) x) * ((double) dwInW) / ((double) iOutW) + 0.5);  
  184.             int y = 0;  
  185.   
  186.             startX = X - nHalfDots;  
  187.             if (startX < 0) {  
  188.                 startX = 0;  
  189.                 start = nHalfDots - X;  
  190.             } else {  
  191.                 start = 0;  
  192.             }  
  193.   
  194.             int stop;  
  195.             int stopX = X + nHalfDots;  
  196.             if (stopX > (dwInW - 1)) {  
  197.                 stopX = dwInW - 1;  
  198.                 stop = nHalfDots + (dwInW - 1 - X);  
  199.             } else {  
  200.                 stop = nHalfDots * 2;  
  201.             }  
  202.   
  203.             if (start > 0 || stop < nDots - 1) {  
  204.                 CalTempContrib(start, stop);  
  205.                 for (y = 0; y < dwInH; y++) {  
  206.                     value = HorizontalFilter(bufImage, startX, stopX, start,  
  207.                             stop, y, tmpContrib);  
  208.                     pbOut.setRGB(x, y, value);  
  209.                 }  
  210.             } else {  
  211.                 for (y = 0; y < dwInH; y++) {  
  212.                     value = HorizontalFilter(bufImage, startX, stopX, start,  
  213.                             stop, y, normContrib);  
  214.                     pbOut.setRGB(x, y, value);  
  215.                 }  
  216.             }  
  217.         }  
  218.   
  219.         return pbOut;  
  220.   
  221.     } // end of HorizontalFiltering()  
  222.   
  223.     private int VerticalFilter(BufferedImage pbInImage, int startY, int stopY,  
  224.             int start, int stop, int x, double[] pContrib) {  
  225.         double valueRed = 0.0;  
  226.         double valueGreen = 0.0;  
  227.         double valueBlue = 0.0;  
  228.         int valueRGB = 0;  
  229.         int i, j;  
  230.   
  231.         for (i = startY, j = start; i <= stopY; i++, j++) {  
  232.             valueRGB = pbInImage.getRGB(x, i);  
  233.   
  234.             valueRed += GetRedValue(valueRGB) * pContrib[j];  
  235.             valueGreen += GetGreenValue(valueRGB) * pContrib[j];  
  236.             valueBlue += GetBlueValue(valueRGB) * pContrib[j];  
  237.             // System.out.println(valueRed+"->"+Clip((int)valueRed)+"<-");  
  238.             //    
  239.             // System.out.println(valueGreen+"->"+Clip((int)valueGreen)+"<-");  
  240.             // System.out.println(valueBlue+"->"+Clip((int)valueBlue)+"<-"+"-->");  
  241.         }  
  242.   
  243.         valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen),  
  244.                 Clip((int) valueBlue));  
  245.         // System.out.println(valueRGB);  
  246.         return valueRGB;  
  247.   
  248.     } // end of VerticalFilter()  
  249.   
  250.     private BufferedImage VerticalFiltering(BufferedImage pbImage, int iOutH) {  
  251.         int iW = pbImage.getWidth();  
  252.         int iH = pbImage.getHeight();  
  253.         int value = 0;  
  254.         BufferedImage pbOut = new BufferedImage(iW, iOutH,  
  255.                 BufferedImage.TYPE_INT_RGB);  
  256.   
  257.         for (int y = 0; y < iOutH; y++) {  
  258.   
  259.             int startY;  
  260.             int start;  
  261.             int Y = (int) (((double) y) * ((double) iH) / ((double) iOutH) + 0.5);  
  262.   
  263.             startY = Y - nHalfDots;  
  264.             if (startY < 0) {  
  265.                 startY = 0;  
  266.                 start = nHalfDots - Y;  
  267.             } else {  
  268.                 start = 0;  
  269.             }  
  270.   
  271.             int stop;  
  272.             int stopY = Y + nHalfDots;  
  273.             if (stopY > (int) (iH - 1)) {  
  274.                 stopY = iH - 1;  
  275.                 stop = nHalfDots + (iH - 1 - Y);  
  276.             } else {  
  277.                 stop = nHalfDots * 2;  
  278.             }  
  279.   
  280.             if (start > 0 || stop < nDots - 1) {  
  281.                 CalTempContrib(start, stop);  
  282.                 for (int x = 0; x < iW; x++) {  
  283.                     value = VerticalFilter(pbImage, startY, stopY, start, stop,  
  284.                             x, tmpContrib);  
  285.                     pbOut.setRGB(x, y, value);  
  286.                 }  
  287.             } else {  
  288.                 for (int x = 0; x < iW; x++) {  
  289.                     value = VerticalFilter(pbImage, startY, stopY, start, stop,  
  290.                             x, normContrib);  
  291.                     pbOut.setRGB(x, y, value);  
  292.                 }  
  293.             }  
  294.   
  295.         }  
  296.   
  297.         return pbOut;  
  298.   
  299.     } // end of VerticalFiltering()  
  300.   
  301.     private int Clip(int x) {  
  302.         if (x < 0)  
  303.             return 0;  
  304.         if (x > 255)  
  305.             return 255;  
  306.         return x;  
  307.     }  
  308.   
  309.     /** 
  310.      * End: Use Lanczos filter to replace the original algorithm for image 
  311.      * scaling. Lanczos improves quality of the scaled image modify by :blade 
  312.      */  
  313.   
  314. }  

 

 

看看第二种效果怎样。。

呵呵,,第二种不错吧! 好了,以上是struts2上传图片生成高清缩略图,接下来就是如何利用spring mvc 来完成这个功能了。。
 
首先spring MVC没有 ServletActionContext.getServletContext().getRealPath(imagePath);  这个获取上下文的方法。你也可以自己写一个,,
 
但可以用request啊。有现成的何必再自己写呢!呵呵        
 
  request.getSession().getServletContext().getRealPath(imagePath); 用这个代替,当然方法逆天必需传入request
 
第二:    
   BufferedImage srcBufferImage = ImageIO.read(getUpload);  这个方法 传入getUpload是file 类型,但spring MVC 中MultipartFile 只有这个类型。所以行不通。查了查API,
 
发现ImageIO.read(getUpload); 可以传入InputStream的。所以只要修改一下multipartFile.getInputStream()就OK。。 最后一点:同样道
 
理 把 FileInputStream fis = new FileInputStream(getUpload);  修改为 FileInputStream fis = (FileInputStream) multipartFile.getInputStream();好吧!贴上源码: ScaleImage
 
这个类不变。。只修改一下UploadUtil

 

 

[java] view plaincopyprint?
  1. package com.lanyuan.upload;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.text.DateFormat;  
  9. import java.text.SimpleDateFormat;  
  10. import java.util.Date;  
  11. import java.util.Random;  
  12.   
  13. import javax.imageio.ImageIO;  
  14. import javax.servlet.http.HttpServletRequest;  
  15.   
  16. import org.springframework.web.multipart.MultipartFile;  
  17.   
  18.   
  19. /** 
  20.  *  
  21.  * 上传图片 工具类 大图片路径,生成小图片路径, 大图片文件名,生成小图片文名, 生成小图片宽度,生成小图片高度, 是否等比缩放(默认为true)) 
  22.  *  
  23.  * @author Administrator 
  24.  *  
  25.  */  
  26. public class UploadUtil  
  27. {  
  28.     private String imagePath = "/imageFile/" + new SimpleDateFormat("yyyyMMddHH").format(new Date()) + "";// 配置图片路径  
  29.   
  30.     /** 
  31.      *  
  32.      * @param getUpload 
  33.      *            路径 
  34.      * @param getUploadContentType 
  35.      *            文件类型 
  36.      * @param getUploadFileName 
  37.      *            原文件名 
  38.      * @return 
  39.      * @throws IOException 
  40.      */  
  41.     public void uploadImage1(HttpServletRequest request,MultipartFile file, String getUploadContentType, String getUploadFileName) throws IOException  
  42.     {  
  43.   
  44.         String getImagePath = request.getSession().getServletContext().getRealPath(imagePath);  
  45.   
  46.         File image = new File(getImagePath);  
  47.         if (!image.exists())  
  48.         {  
  49.             image.mkdir();  
  50.         }  
  51.   
  52.         // 得到上传文件的后缀名  
  53.         String uploadName = getUploadContentType;  
  54.         System.out.println("图片类型 ------------" + uploadName);  
  55.   
  56.         String lastuploadName = uploadName.substring(uploadName.indexOf("/") + 1, uploadName.length());  
  57.         System.out.println("得到上传文件的后缀名 ------------" + lastuploadName);  
  58.   
  59.         // 得到文件的新名字  
  60.         String fileNewName = generateFileName(getUploadFileName);  
  61.         System.out.println("// 得到文件的新名字 ------------" + fileNewName);  
  62.   
  63.         // FileOutputStream fos = new FileOutputStream(getImagePath + "/" +  
  64.         // fileNewName);  
  65.         //        
  66.         // FileInputStream fis = new FileInputStream(getUpload);  
  67.         // byte[] buffer = new byte[1024];  
  68.         // int len = 0;  
  69.         // while ((len = fis.read(buffer)) > 0) {  
  70.         // fos.write(buffer, 0, len);  
  71.         // }  
  72.   
  73.         // 最后返回图片路径  
  74.         imagePath = imagePath + "/" + fileNewName;  
  75.         System.out.println(" 回图片路径   " + file.getInputStream());  
  76.         System.out.println("        //最后返回图片路径   " + imagePath);  
  77.   
  78.         BufferedImage srcBufferImage = ImageIO.read(file.getInputStream());  
  79.         System.out.println(" w " + srcBufferImage.getWidth() + " w " + srcBufferImage.getHeight());  
  80.         BufferedImage scaledImage;  
  81.         ScaleImage scaleImage = ScaleImage.getInstance();  
  82.         int yw = srcBufferImage.getWidth();  
  83.         int yh = srcBufferImage.getHeight();  
  84.         int w = 400, h = 300;  
  85.         // 如果上传图片 宽高 比 压缩的要小 则不压缩  
  86.         if (w > yw && h > yh)  
  87.         {  
  88.             FileOutputStream fos = new FileOutputStream(getImagePath + "/" + fileNewName);  
  89.   
  90.             FileInputStream fis = (FileInputStream) file.getInputStream();  
  91.             byte[] buffer = new byte[1024];  
  92.             int len = 0;  
  93.             while ((len = fis.read(buffer)) > 0)  
  94.             {  
  95.                 fos.write(buffer, 0, len);  
  96.             }  
  97.         }  
  98.         else  
  99.         {  
  100.             scaledImage = scaleImage.imageZoomOut(srcBufferImage, w, h);  
  101.             FileOutputStream out = new FileOutputStream(getImagePath + "/" + fileNewName);  
  102.             ImageIO.write(scaledImage, "jpeg", out);  
  103.   
  104.         }  
  105.     }  
  106.   
  107.     /** 
  108.      * 传入原图名称,,获得一个以时间格式的新名称 
  109.      *  
  110.      * @param fileName 
  111.      *             原图名称 
  112.      * @return 
  113.      */  
  114.     private String generateFileName(String fileName)  
  115.     {  
  116.         DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");  
  117.         String formatDate = format.format(new Date());  
  118.         int random = new Random().nextInt(10000);  
  119.         int position = fileName.lastIndexOf(".");  
  120.         String extension = fileName.substring(position);  
  121.         return formatDate + random + extension;  
  122.     }  
  123.   
  124.     public String getImagepath()  
  125.     {  
  126.         return imagePath;  
  127.     }  
  128.   
  129. }  


 

 

 

 

    最后如何调用:请看:很简单

[java] view plaincopyprint?
  1. @RequestMapping ("upload")  
  2.     public String upload(Model model,HttpServletRequest request)  
  3.     {  
  4.         //转型为MultipartHttpRequest(重点的所在)     
  5.                 MultipartHttpServletRequest multipartRequest  =  (MultipartHttpServletRequest) request;     
  6.                  //  获得第1张图片(根据前台的name名称得到上传的文件)      
  7.                 MultipartFile imgFile1  =  multipartRequest.getFile("imgFile");     
  8.                   
  9.                 UploadUtil uploadutil = new UploadUtil();  
  10.                 String fileName = imgFile1.getOriginalFilename();  
  11.                 try {  
  12.                 uploadutil.uploadImage1(request, imgFile1, imgFile1.getContentType(), fileName);  
  13.                 } catch (IOException e) {  
  14.                     // TODO Auto-generated catch block  
  15.                     e.printStackTrace();  
  16.                 }  
  17.         return "redirect:query";  
  18.     } 
0 0
原创粉丝点击