文件上传与图片自动压缩. 
 本程序只适合gif,jpg,png图片格式. 上传大小不要最好不要超过1M. 程序可对图片进行统一大小压缩. 如让所有上传图 宽度处理成宽度为500像数的图片. 高度可由程序自动设置. 并可对上传图片同进生成缩略图. 如 宽度为了100 
为了主程序运行. 需要下载 apache.commons.fileupload 组件. (www.apache.org) 
大部分操作以由二个类完成.只要在Jsp或servlet 中加入很少的代码就可以完成工作了.  
 <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> 
<html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> 
<body> <jsp:useBean id="upload" scope="page" class="com.date.myupload"/> <%                      String root_d=request.getRealPath("");      //取得应用目录.   String root_dir=root_d+"/test";             //上传目录.               upload.setTempdir(root_dir);                //设置临时目录               upload.setUpsize(4194304);                  //上传文件大小   upload.setUpbuffer(4096);                   //缓冲大小      upload.upload(request);                    //分析请求.      out.print(upload.getFiledvalue("user"));     //取得表单值.   out.print("<br>");   upload.setImgproce(true,500,"www.ssmei.com");    //设置图片处理参数. true 为要进行处理. 500为图片外理后的宽度.                                                                 //"www.ssmei.com"是在图片上打上签名.   out.print(upload.savaFile("thisfile",root_dir+"/a.jpg"));    //保荐文件到服务器上.   thisfile是文件表单名字.                                                                             //root_dir+"/a.jpg 为路径.       upload.setImgproce(true,100,null);                           //第二个文或保存为不同大小的图片. 一个文件可多次处理和何保存.   out.print(upload.savaFile("thisfile",root_dir+"/a1.jpg"));            upload.delete();    %> 
</body> </html> //===================================================       图片压缩类  //=================================================== package com.date; 
import java.io.File; import java.io.FileOutputStream; import java.awt.Graphics; import java.awt.Image; import java.awt.*; import java.awt.image.BufferedImage; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; 
public class imgproce {   private int wideth;   private int height;   private String t=null; public void setT(String t)   {     this.t=t;   } public void setWideth(int wideth)   {     this.wideth=wideth;   } public int getWideth()   {     return this.wideth;   } public void setHeight(int height)   {     this.height=height;   } public int getHeight(int w,int h)                     //former images size   {     int hhh;     if(w > wideth)     {       float ww;       ww = (float) w / (float) wideth;       float hh = h / ww;       return (int) hh;     }     else     {       this.setWideth(w);       return h;     } 
  } public void proce(String fpath) throws Exception   {    File _file = new File(fpath);    Image src = javax.imageio.ImageIO.read(_file);    int wideth=src.getWidth(null);    int height=src.getHeight(null);    int h=this.getHeight(wideth,height);    BufferedImage tag = new BufferedImage(this.getWideth(),h,BufferedImage.TYPE_INT_RGB);    Graphics g=tag.getGraphics();    g.drawImage(src, 0, 0, this.getWideth(), h, null);      if(t!=null)       {         g.setColor(new Color(242,242,242));         g.fillRect(this.getWideth() - 120, h - 18,120,18);         g.setColor(new Color(180,180,180));         g.drawRect(this.getWideth() - 120, h - 18,119,17);         g.setColor(new Color(255,102,0));         g.drawString(t, this.getWideth() - 100, h - 5);       }    FileOutputStream out=new FileOutputStream(fpath);    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    encoder.encode(tag);    out.close();   } } 
  
//===================================================       文件上传类 //=================================================== 
package com.date; 
import javax.servlet.http.*; import org.apache.commons.fileupload.*; import java.util.*; import java.io.*; 
public class myupload {   private  DiskFileUpload fu;   private List item;   private  imgproce imgpro;   private String t=null;   private boolean proce;   private int w;   public void setImgproce(boolean proce,int w,String t)   {     this.proce=proce;     this.w=w;     this.t=t;   }   public myupload()   {    fu = new DiskFileUpload();    imgpro=new imgproce();   }   public void setUpsize(int size)   {     fu.setSizeMax(size);   }   public void setUpbuffer(int bsize)   {     fu.setSizeThreshold(bsize);   }   public void setTempdir(String dir)   {    fu.setRepositoryPath(dir);   } 
  public void upload(HttpServletRequest request)   {     try{           item = fu.parseRequest(request);         }     catch(Exception e)     { System.out.println("upload paser error"); } 
  } 
  public String getFiledvalue(String filedname)   {     String value="";     Iterator i = item.iterator();     while(i.hasNext())     { 
      FileItem fi = (FileItem) i.next(); 
      if (fi.isFormField())       {         if(fi.getFieldName().equals(filedname))            {              value=fi.getString();            }       }     }    return value;   } 
  public String savaFile(String filedname,String dir)   {     String fname="";     try{          Iterator i = item.iterator();         while (i.hasNext())          {          FileItem fi = (FileItem) i.next();          if (fi.isFormField())            {            }          else            {           if (fi.getFieldName().equals(filedname)&fi.getSize()>0)              {                 fi.write(new File(dir));                 if(proce)                   {                    imgpro.setWideth(w);                    imgpro.setT(t);                    imgpro.proce(dir);                   }              }            }         }     }     catch(Exception e)     { System.out.println("upload sava error!");}    return dir;   } 
  public void delete() throws Throwable     { 
     Iterator i = item.iterator();      while(i.hasNext())        {          FileItem fi = (FileItem) i.next();          fi.delete();        } 
     System.out.println("close upload");     } 
} 
小辉:[email protected]  
 
  |