| Blog信息 |
|
blog名称:网络爬虫 日志总数:39 评论数量:202 留言数量:2 访问次数:250221 建立时间:2005年3月22日 |

| |
|
[JSP应用]JSP上传图片并生成缩略图 软件技术, 电脑与网络
网络爬虫 发表于 2005/5/11 15:58:02 |
|
JSP上传图片并生成缩略图
本例子使用了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> |
|
|
回复:JSP上传图片并生成缩略图 软件技术, 电脑与网络
fgfh(游客)发表评论于2007/6/28 13:02:14 |
| 网站做的真好!公益[url=http://www.zs188.com/article/11/]入党申请书[/url]网与贵站交换友情连接QQ:809597549 |
|
|
回复:JSP上传图片并生成缩略图 软件技术, 电脑与网络
blxu(游客)发表评论于2006/11/2 14:31:48 |
| 本机服务器没有问题,在网上服务器测试的时候报错:com.jspsmart.upload.SmartUploadException: File can't be saved (1120).
出错的语句是:myFile.saveAs(saveurl,mySmartUpload.SAVE_PHYSICAL);
急啊,能告诉我怎么解决吗? |
|
|
回复:JSP上传图片并生成缩略图 软件技术, 电脑与网络
不谢(游客)发表评论于2005/12/5 17:58:07 |
|
|
回复:JSP上传图片并生成缩略图 软件技术, 电脑与网络
JSP爱好者(游客)发表评论于2005/11/21 13:46:32 |
|
|
回复:JSP上传图片并生成缩略图 软件技术, 电脑与网络
bb(游客)发表评论于2005/10/10 18:34:35 |
| com.jspsmart.upload.File@15856a5java.lang.IllegalArgumentException: This path does not exist (1135).错误报出。
uploadfile/images/是相对应用服务器下的路径吗。谢谢 |
|
|
回复:JSP上传图片并生成缩略图 软件技术, 电脑与网络
aa(游客)发表评论于2005/10/10 18:32:45 |
|
|
回复:JSP上传图片并生成缩略图 软件技术, 电脑与网络
FF(游客)发表评论于2005/9/20 14:51:18 |
| 找了很久,做缩略图还是这个老方法,怎么没人注意到生出来的新图的质量非常地差呢?:(到现在还不知道如何解决! |
|
|
回复:JSP上传图片并生成缩略图 软件技术, 电脑与网络
apin(游客)发表评论于2005/9/11 14:25:09 |
| http://apin.xhschool.com/user1/532/archives/2005/3885.shtml
文章中还修改了一个小bug,关于下载时中文的显示问题。 |
|
|
回复:JSP上传图片并生成缩略图 软件技术, 电脑与网络
wdnmg(游客|211.99.151.206)发表评论于2005/8/3 16:52:12 |
| 你好,我正在做一个JSP网站,看到你的文章,正是我需要的,但是www.jspsmart.com网站已经关闭了,能把你那的jspsmart组件和com.sun.image.codec.jpeg.JPEGImageEncoder类给我发一份嘛!不盛感激 wdnmg@163.com |
|
|
回复:JSP上传图片并生成缩略图 软件技术, 电脑与网络
a(游客|61.141.202.4)发表评论于2005/8/3 14:27:22 |
|
|