本例子使用了jspsmart组件进行上传,这里可以免费下载该组件
http://blogimg.chinaunix.net/blog/upfile/070424131834.rar
下载解压后,将jar包复制到 /WEB-INF/lib 目录后重启服务器,jspsmart即可正常使用了。
1、uploadimage.jsp
<%@ page language="java" pageEncoding="UTF-8" language="java" import="com.jspsmart.upload.*,java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,java.util.*,java.util.Calendar"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<BODY BGCOLOR="white">
<H1>图片上传成功!</H1>
<HR>
<%
long file_size_max = 4000000;
String fileName2 = "", ext = "", testvar = "";
String url = "image2/pptImages/"; //应保证在根目录中有此目录的存在
//初始化
mySmartUpload.initialize(pageContext);
//只允许上载此类文件
try {
mySmartUpload.setAllowedFilesList("jpg,gif,jpeg,png");
//上载文件
mySmartUpload.upload();
} catch (Exception e) {
%>
alert("只允许上传.jpg和.gif类型图片文件");
window.location=''upload.jsp'';
</script>
<%
}
try {
com.jspsmart.upload.File myFile = mySmartUpload.getFiles()
.getFile(0);
if (myFile.isMissing()) {
%>
<SCRIPT language=javascript>
alert("请先选择要上传的文件");
window.location=''upload.jsp'';
</script>
<%
}
else {
//String myFileName=myFile.getFileName(); //取得上载的文件的文件名
ext = myFile.getFileExt(); //取得后缀名
int file_size = myFile.getSize(); //取得文件的大小
String saveurl = "";
String imageURL = "";
if (file_size < file_size_max) {
//更改文件名,取得当前上传时间的毫秒数值
Calendar calendar = Calendar.getInstance();
String filename = String.valueOf(calendar
.getTimeInMillis());
saveurl = request.getRealPath("/") + url;
//out.println(saveurl);
saveurl += filename + "." + ext; //保存路径
imageURL = "image2/pptImages/" + filename + "." + ext;
myFile.saveAs(saveurl, mySmartUpload.SAVE_PHYSICAL);
//将图片地址返回到的父窗口地址框中.
out
.print("<script language='javascript' type='text/javascript'>if(window.opener)window.opener.document.getElementById('photo').value = '"
+ imageURL + "'</script>");
out.print("<script>window.close(); </script>");
//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();
}
%>
</BODY>
</HTML>
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>
下面是我的项目中用到的:
当点击那个“checkbox”时候,会弹出一个窗口:
当上传图片完成时候会将连接返回到父窗口的图片地址文本框中。见下图:
下面是这一个功能的实现代码:
//用于显示或者隐藏上传图片的文本框
function displayUpLoadImage(){
var chbox = document.getElementById("isPPTPics");
var displayInputBox = document.getElementById("photoinput");
if(chbox.checked == true){
displayInputBox.style.display = "block";
window.open ('/tjedu/Admin/upload.jsp', 'uploadwindow', 'height=200, width=500, top=200, left=200, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no');
}else{
displayInputBox.style.display = "none";
}
}
//父窗口中的图片地址文本框代码:
<input name="isPPTPics" id="isPPTPics" type="checkbox" onClick="displayUpLoadImage();" value="PPTPics" />是否显示为图片新闻
<div id="photoinput" style="display:none;" >
<input type="text" name="photo" id="photo" size=50 />
</div>
总之还是挺简单的,大家有什么问题可以和我交流:qq:306456882 :-)