学习网考试学习资料

Gzu521.com

JSP上传图片并生成缩略图

Java教程   点击:次   发布时间:2005-11-15   【字体: 】   来源:Gzu521.com
贵 州 学 习 网
 

本例子使用了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());

aveurl=request.getrealpath("/")+url;

aveurl+=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;

}

ew_w=math.round(old_w/tempdouble);

ew_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编码

ewimage.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> Z"XgnX`odTki? qWQ5 [ 本 资 料 来 源 于 贵 州 学 习 网 网络编程Java教程 http://Www.gzU521.com ] Z"XgnX`odTki? qWQ5

责任编辑:gzu521

网络编程分类
ASP教程
.Net教程
Java教程
PHP教程
数据库基础
ACCESS教程
SQL Server教程
MySQL教程
Oracle教程
分类推荐信息
更多...
大类最新文章
更多...