上传图1.jsp
<table id="file_pic">
<tr>
<td><img id="ctl00_cphMain_Pimg_1" src="${hereCorporate.corpLogoThumb }" alt="nopic" width="100" height="100" style="border-width:1px;border-style:solid;" οnlοad="javascript:DrawImage(this);"/> </td>
</tr>
<tr>
<td>
<input type="button" οnclick="showUpload('3')" id="btn_up_3" value="上传/>" />
<input type="button" οnclick="delImg('3')" value="删除"/>
<div id="up" style="display:none">
<iframe id="Iframe_Upload" src="2.jsp" width="550px" height="50" scrolling="no" frameborder='0' bordercolor="red">
</iframe>
</div>
</td>
</tr>
</table>
上传图片2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.util.*,com.jspsmart.upload.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<table border="0" align="left" cellpadding="0" cellspacing="0">
<tr>
<td height="45" align="center" valign="middle">
<form action="corporate_upload.jsp" method="post" enctype="multipart/form-data" name="form2">
<input type="file" name="file">
<input type="submit" name="Submit" value="上传">
</form>
</td>
</tr>
</table>
</body>
</html>
上传图片3.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK" import="java.util.*,com.jspsmart.upload.*" %>
<%
SmartUpload mySmartUpload =new SmartUpload();//创建一个新的上传对象
long file_size_max=2000000; //单位为字节1048576为1兆
String fileName2=""; //定义文件名为字符串的一个属性
String ext=""; //定义文件名后缀为字符串的一个属性
String testvar=""; //
String url="upload/"; //应保证在根目录中有此目录的存在(也就是说需要自己建立相应的文件夹)
//初始化
mySmartUpload.initialize(pageContext);
//只允许上载此类文件
try {
// 1.限制每个上传文件的最大长度。
// mySmartUpload.setMaxFileSize(10000);
// 2.限制总上传数据的长度。
//mySmartUpload.setTotalMaxFileSize(100*50);
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
// mySmartUpload.setAllowedFilesList("doc,txt");
mySmartUpload.setDeniedFilesList("exe,bat,jsp,htm,html,,"); // 设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
mySmartUpload.setAllowedFilesList("jpg,JPG,jpeg,JPEG");//此处的文件格式可以根据需要自己修改
//上载文件
mySmartUpload.upload();//开始上传
} catch (Exception e){
%>
<SCRIPT language=javascript>
alert("只允许上传.jpg和.jpeg类型图片文件");
window.location='2.jsp';
</script>
<%
}
try{
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);//获得2.jsp中file的文件名字符串
if (myFile.isMissing()){//如果没有文件名执行下面的javascript
%>
<SCRIPT language=javascript>
alert("请先选择要上传的文件");
window.location='2.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());//将时间的毫秒作为文件名字的字符串
String fname=filename+"."+ext;//文件名加后缀名组成一个新的文件名
saveurl=application.getRealPath("/")+url;//或的一个真实的路径
saveurl+=filename+"."+ext; //新路径加文件名加后缀组成新的路径
myFile.saveAs(saveurl,SmartUpload.SAVE_PHYSICAL);//保存路径
//out.print(saveurl);
%>
<script language=javascript>
var pid=parent.document.getElementById('paramid').value;//获得父类隐藏域的paramid的value值
var updiv=parent.document.getElementById("up");//获得父类up(ID)并肤质为updiv
updiv.style.display ="none";//将id为updiv的css属性设置为隐藏(none);
parent.num=1;//父类的图片数为一
parent.document.getElementById("btn_up_"+pid).value="替换";//获得父类的btn_up_pid(id)的值并将值设置为替换
<%
//out.print("parent.document.getElementById(\"img\"+pid).value=\""+fname+"\";");
//将文件的路径赋给img 的src
out.print("var img=parent.document.getElementById('ctl00_cphMain_Pimg_1'); " + "img.src=\"" + request.getContextPath() + "/upload/" + filename + "." + ext + "\";");
//将路径赋给隐藏域的img(id)的value
out.print("var img=parent.document.getElementById('img0').value=\"" + filename + "." + ext + "\";");
%>
window.location='2.jsp';
</script>
<%
}else{
out.print("<script language=javascript>alert('图片过大');</script>");
}
}
}catch (Exception e){
out.print(e.toString());
}
%>