jsp用jspsmart上传后自动生存成缩略图

chenhongxin 2009-05-26 10:17:34
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>

...全文
68 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
铁匠梁 2009-05-27
  • 打赏
  • 举报
回复
其实可以在上传的时候就可以使用JS脚本预览
Landor2004 2009-05-27
  • 打赏
  • 举报
回复
收藏一下!
lgq_0714 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用楼主 chenhongxin 的帖子:]
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="uploadfil…
[/Quote]
谢谢LZ分享!
Forever_He 2009-05-27
  • 打赏
  • 举报
回复
学习...
qiheia 2009-05-26
  • 打赏
  • 举报
回复
请LZ是你要问什么问题吗

还是说明这些代码能成功运行,给人学习示范?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧