做应用开发,无论是前台还是后台都离不开对图片的上传和下载,这样不仅有利于用户之间的交流,共享,把图片下载到本地后更有利于观看浏览,减少网上数据通信量,提高浏览速度。在开发中我就遇到了对图片的操作,后台需要将对应的图片上传到服务器上,最后在客户端查看显示。在此过程中,用的是SmartUpload组件,简单实用。同时,也可以尝试fileUpload,也可以实现上传下载功能,只是使用起来很麻烦,所以建议用SmartUpload组件。下面就详细介绍下smartupload的用法。以下将实现这样的功能:将一张大图片上传到服务器上,并压缩成一张小尺寸的,以相同的名称分别存在两个文件夹下,需要显示的时候可以分别读取各自文件夹。好了,现在开始实现详细过程如下:
smartupload本身是系统提供的一个jar包,用户可以直接将包放到classpath下,也可以将包直接复制到TOMCAT_HOME\lib目录中,而且必须使用HTML提供的file控件,<form>也必须用enctype进行封装,表示表单将按照二进制的方式提交。因此其他的非表单控件的内容便无法使用request获取了,必须通过Smartupload类中提供的getRequest()方法来取得全部的请求参数。
在aa.jsp中实现浏览选择图片功能:主要代码:<form name="form" action="Add.jsp" method="post" enctype="multipart/form-data" οnsubmit="return yan()" >
<tr> <td nowrap align="right" width="13%">选择图像:</td> <td width="41%"> <input id="assphoto" name="badgeimage" type="file" size="40" style="width:250px" class="text"/> <span class="red">图片为100*100</span> </td> </tr>
<input type="submit" value="上传" class="button" /> <input type="reset" value="取消" class="button"/>
</form>
在Add.jsp中实现上传功能。
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ page import="com.jspsmart.upload.*" %> <%@ page import="com.Upload.PictureName.IPTimeStamp" %> <%@ page import="java.io.File"%> <%@ page import="java.applet.Applet" %> <%@ page import="java.awt.*" %> <%@ page import="javax.imageio.*" %> <%@ page import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*, java.sql.*,com.jspsmart.upload.*,java.util.*"%>
<jsp:useBean id="dataOption" class="com.Carrie.dao.DataOption"></jsp:useBean> <jsp:useBean id="trans" class="com.Carrie.common.Trans"></jsp:useBean> <html> <head> </head> <body> <% String bigimageAddress=application.getRealPath("/"); //
String smallimageAddress="uploadimage\\bigImage\\";//原图保存的路径 String smallimageAddress1="uploadimage\\smallImage\\";//压缩后小图片的保存的根目录的路径
SmartUpload smart = new SmartUpload(); smart.initialize(pageContext); smart.upload(); String badgeid=smart.getRequest().getParameter("badgeid"); String badgename=smart.getRequest().getParameter("badgename"); String badgeexplain=smart.getRequest().getParameter("badgeexplain");
IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()); //生成的一组随机数 String ext = smart.getFiles().getFile(0).getFileExt(); //获取的上传文件的格式 String fileName = its.getIPTimeRand()+"."+ext; //对上传的文件重新命名,若前后两文件重名则后者把前者覆盖 // System.out.println(fileName); smart.getFiles().getFile(0).saveAs(bigimageAddress+smallimageAddress+fileName); //原图保存 String playerheadadr = bigimageAddress+smallimageAddress+fileName; String url=bigimageAddress+smallimageAddress1; java.io.File file = new java.io.File(playerheadadr); String newurladd = url+fileName; Image src = javax.imageio.ImageIO.read(file); // 图片压缩 float tagsize=50; 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);//计算新图长宽 Graphics2D g2D=null; BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_4BYTE_ABGR_PRE); tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图 ImageIO.write(tag, "png", new File(newurladd)); //压缩后保存新图 /*g2D = (Graphics2D) tag.getGraphics(); g2D.drawImage(src, 0, 0, null); for(int i=0;i<new_w;i++){ for(int j=0;j<new_h;j++){ int rgb=tag.getRGB(i, j); if(isBackPixel(rgb)){ tag.setRGB(i, j,0); } } } g2D.drawImage(tag, 0, 0, null); */ // FileOutputStream newimage=new FileOutputStream(newurladd); //输出到文件流 // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage); // encoder.encode(tag); //近JPEG编码 // newimage.close(); String badgeimage = fileName; // out.println(newurladd);
try{ int outputflag=dataOption.InsertBadgeInfo(badgeid,badgename,badgeexplain,badgeimage); //添加到数据库并得到返回值判断 if(outputflag==2){ out.print("<h2>"+"该图已被添加!!!"+"</h2>"); %> <input type="button" value="返回" class="button" οnclick="window.history.go(-1)"/> <% }else if(outputflag==3){ out.print("<h2>"+"图片添加失败!!!"+"</h2>"); %> <input type="button" value="返回" class="button" οnclick="window.history.go(-1)"/> <% }else { response.sendRedirect("../lBdge.jsp"); } }catch(Exception e){ e.printStackTrace(); } %> </body> </html>
说明:对于其中的图片压缩的方法,网上都有很详细的案例。这里的压缩成的图片并不是高清的,没有原图清晰,有锯齿状的边,这种方法不完美,还需要继续处理!
同时附上IPTimeStamp类,这是从网上查找,最后综合成的。个人觉得可以自己写个简单的,完全不用这么麻烦!地址:http://download.csdn.net/detail/kdsde/5130297 |