jsp上传图片并生成缩略图
本例子使用了jspsmart组件进行上传,这里可以免费下载该组件www.jspsmart.com
下载解压后,将jar包复制到 web-inflib 目录后重启服务器,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>
新闻热点
疑难解答