一下3个是俺在网上找的,都没有验证过,过些日子可能会用,到时候再验证一下。不过看看问题应该不大。
----------------------------------------------------------------------------------------------------------------------------------
在某些网站,我们往往需要上传一些图片资料。但随着高分辨率DC的普及,上传的图片容量会很大,比如300万象素DC出来的文件基本不下 600K。
为了治理方便,大家可能不愿意每次都用ACDsee修改它,而直接上传到服务器。但是这种做法在客户端看来就没有那么轻松了,对于拨号上网的用户简直是一场恶梦,虽然你可以在图片区域设置wide和high! 问题的解决之道来了!我们可以在类中处理一张大图,并缩小它。 前提是需要JDK1.4,这样才能进行处理。按以下方法做:
import Java.io.File; import java.io.FileOutputStream; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class JpgTest { public void JpgTset() throws Exception{ File _file = new File("/Order005-0001.jpg"); //读入文件 Image src = javax.imageio.ImageIO.read(_file); //构造Image对象 int wideth=src.getWidth(null); //得到源图宽 int height=src.getHeight(null); //得到源图长 BufferedImage tag = new BufferedImage(wideth/2,height/2,BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src,0,0,wideth/2,height/2,null); //绘制缩小后的图 FileOutputStream out=new FileOutputStream("newfile.jpg"); //输出到文件流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); //近JPEG编码 //System.out.print(width+"*"+height); out.close(); } }
过程很简单,从本地磁盘读取文件Order005-0001.jpg(2032*1524),变成Image对象src,接着构造目标文件tag,设置tag的长宽为源图的一半,对tag进行编码,输出到文件流out,最后关闭文件流。 还有一些问题需要说明: 第一,目前只能支持JPG(JPEG)、GIF、PNG三种格式。 第二,对于源图的容量有限制,最好不要超过1M,否则会抛内存不足的错误,不过我试验过1.8M的源图,可以成功,但是也很轻易抛内存不足。
--------------------------------方法2---------------------------------------------------------------------------------------------------
if(System.IO.File.Exists(path)) { goodsid+=dt.Rows[i]["goodsid"].ToString()+",";// if(!System.IO.File.Exists(Picpath))// { System.Drawing.Image img=System.Drawing.Image.FromFile(path); //生成中图 int w=img.Width; //当前图片的宽 int h=img.Height; //当前图片的高 int x=0; //设置生成图片的宽 int y=0; //设置生成图片的高 int curx=182; int cury=122; if(w>=curx && h<=cury) { x=curx ; y=curx *h/w; } else if(h>=cury&& w<=curx ) { y=cury; x=cury*w/h; } else if(h>=cury&& w>=curx ) { if(h/cury<w/curx ) { x=curx ; y=curx *h/w; } else { y=cury; x=cury*w/h; } } else if(h<=cury&& w<=curx ) { x=img.Width; y=img.Height; } System.Drawing.Image newimg=img.GetThumbnailImage(x,y,null,new System.IntPtr(0)); newimg.Save(Picpath,System.Drawing.Imaging.ImageFormat.Jpeg); img.Dispose(); newimg.Dispose();
--------------------------------例子---------------------------------------------------------------------------------------------------
本例子使用了jspsmart组件进行上传,这里可以免费下载该组件www.jspsmart.com下载解压后,将jar包复制到 /WEB-INF/lib 目录后重启服务器,jspsmart即可正常使用了1、uploadimage.jsp<%@ page contentType="text/html;charset=gb2312" language="java" import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*,cn.oof.database.*,cn.oof.house.*"%><%SmartUpload mySmartUpload =new SmartUpload();long file_size_max=4000000;String fileName2="",ext="",testvar="";String url="uploadfile/images/"; //应保证在根目录中有此目录的存在//初始化mySmartUpload.initialize(pageContext);//只允许上载此类文件try {mySmartUpload.setAllowedFilesList("jpg,gif");//上载文件 mySmartUpload.upload();} catch (Exception e){%> <SCRIPT language=javascript> alert("只允许上传.jpg和.gif类型图片文件"); window.location=''upfile.jsp''; </script><%}try{ com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0); if (myFile.isMissing()){%> <SCRIPT language=javascript> alert("请先选择要上传的文件"); window.location=''upfile.jsp''; </script> <%} else{ //String myFileName=myFile.getFileName(); //取得上载的文件的文件名 ext= myFile.getFileExt(); //取得后缀名 int file_size=myFile.getSize(); //取得文件的大小 String saveurl=""; if(file_size<file_size_max){ //更改文件名,取得当前上传时间的毫秒数值 Calendar calendar = Calendar.getInstance(); String filename = String.valueOf(calendar.getTimeInMillis()); saveurl=request.getRealPath("/")+url; saveurl+=filename+"."+ext; //保存路径 myFile.saveAs(saveurl,mySmartUpload.SAVE_PHYSICAL); //out.print(filename);//-----------------------上传完成,开始生成缩略图------------------------- java.io.File file = new java.io.File(saveurl); //读入刚才上传的文件 String newurl=request.getRealPath("/")+url+filename+"_min."+ext; //新的缩略图保存地址 Image src = javax.imageio.ImageIO.read(file); //构造Image对象 float tagsize=200; int old_w=src.getWidth(null); //得到源图宽 int old_h=src.getHeight(null); int new_w=0; int new_h=0; //得到源图长 int tempsize; float tempdouble; if(old_w>old_h){ tempdouble=old_w/tagsize; }else{ tempdouble=old_h/tagsize; } new_w=Math.round(old_w/tempdouble); new_h=Math.round(old_h/tempdouble);//计算新图长宽 BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图 FileOutputStream newimage=new FileOutputStream(newurl); //输出到文件流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage); encoder.encode(tag); //近JPEG编码 newimage.close(); } else{ out.print("<SCRIPT language=''javascript''>"); out.print("alert(''上传文件大小不能超过"+(file_size_max/1000)+"K'');"); out.print("window.location=''upfile.jsp;''"); out.print("</SCRIPT>"); } }}catch (Exception e){e.toString();}%> 2 upload.htm<html><head><title>请选择上传的图片</title></head> <body><table border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="45" align="center" valign="middle"><form action="uploadimage.jsp" method="post" enctype="multipart/form-data" name="form1">请选择上传的图片 <input type="file" name="file"><input type="submit" name="Submit" value="上传"> </form></td> </tr></table></body></html>