📄 upload.jsp
字号:
<%@ page language="java" pageEncoding="GB2312" contentType="text/html;charset=GB2312" %>
<%@ page import="cn.com.xunuo.util.PropertyManager"%>
<%
//Server.ScriptTimeOut = 1800
//参数变量
String sType = null;//文件类型
String sStyleName = null; //样式类型
//设置变量
String sAllowExt = null;//允许的扩展名
long lAllowSize = 0l;//允许的文件大小
String sUploadDir = null ;//上传文件路径
int nUploadObject = 0 ; //上传文件的数量
int nAutoDir = 0;//是否自动选择目录
String sBaseUrl = null; //路径形式 "0" : 相对路径 "1": 根路径格式 "2":存储完整访问路径
String sContentPath = null;//附件目录
//接口变量
String sFileExt;//文件扩展名
String sOriginalFileName = null;//源文件名
String sSaveFileName = null;//存储文件名
String sPathFileName = null;//文件路径
int nFileNum = 0; //文件数量
//Call DBConnBegin() ' 初始化数据库连接
//Call InitUpload() ' 初始化上传变量
//Call DBConnEnd() ' 断开数据库连接
String sAction = null;
sAction = request.getParameter("action");
sAction = sAction!=null?sAction.trim().toLowerCase():sAction;
sType = request.getParameter("type");
sStyleName = request.getParameter("style");
sUploadDir = PropertyManager.getProperty("Cms.UploadPath");//保存路径
sBaseUrl = "1";
sContentPath = PropertyManager.getProperty("Cms.UploadPath");
if (sBaseUrl.equals("0"))
{
sContentPath = "" ;
}
if (sBaseUrl.equals("1"))
{
sContentPath = sUploadDir ;
}
if (sBaseUrl.equals("2"))
{
sContentPath = "完整访问路径";
}
//设置允许上传的文件扩展名
if (sType.equals("remote"))
{
sAllowExt = "gif|jpg|jpeg|bmp|png";
lAllowSize = 100*1024*1024;//10M
}else if (sType.equals("file"))
{
sAllowExt = "rar|zip|exe|doc|xls|chm|hlp";
lAllowSize = 10*1024*1024;//10M
}else if (sType.equals("flash"))
{
sAllowExt = "swf";
lAllowSize = 100*1024*1024;//100M
}else if (sType.equals("media"))
{
sAllowExt = "rm|mp3|wav|mid|midi|ra|avi|mpg|mpeg|asf|asx|wma|mov|rmvb";
lAllowSize = 1000*1024*1024;//100M
}else if (sType.equals("image"))
{
sAllowExt = "gif|jpg|jpeg|bmp|png";
lAllowSize = 100*1024*1024;//100M
}else
{
sAllowExt = "gif|jpg|jpeg|bmp|png";
lAllowSize = 100*1024*1024;//100M
}
/*Select Case sAction
Case "REMOTE"
Call DoRemote() ' 远程自动获取
Case "SAVE"
Call ShowForm() ' 显示上传表单
Call DoSave() ' 存文件
Case Else
Call ShowForm() ' 显示上传表单
End Select
public void ShowForm()
{*/
if (sAction!=null && sAction.equals("remote"))
{
/**如果设置了远程文件自动保存,提交到服务器前,会先将远程文件保存到本地:如图片可以先保存到本地
*如有链接:http://localhost:8080/test.gif
*程序会先将数据存取到指定目录下,然后将路径改成Editor设置的路径*/
}else
{
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<HTML>
<HEAD>
<TITLE>文件上传</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
body, a, table, div, span, td, th, input, select{font:9pt;font-family: "宋体", Verdana, Arial, Helvetica, sans-serif;}
body {padding:0px;margin:0px}
</style>
<script language="JavaScript" src="./dialog/dialog.js"></script>
</head>
<body bgcolor="menu">
<form action="upload.jsp?action=save&type=<%=sType%>&style=<%=sStyleName%>" method="post" name="myform" enctype="multipart/form-data">
<input type=file name="uploadfile" size=1 style="width:100%" onchange="originalfile.value=this.value">
<input type="hidden" name="originalfile" value="">
</form>
<script language=javascript>
var sAllowExt = "<%=sAllowExt%>";
// 检测上传表单
function CheckUploadForm() {
if (!IsExt(document.myform.uploadfile.value,sAllowExt)){
parent.UploadError("提示:\n\n请选择一个有效的文件,\n支持的格式有("+sAllowExt+")!");
return false;
}
return true
}
// 提交事件加入检测表单
var oForm = document.myform ;
oForm.attachEvent("onsubmit", CheckUploadForm) ;
if (! oForm.submitUpload) oForm.submitUpload = new Array() ;
oForm.submitUpload[oForm.submitUpload.length] = CheckUploadForm ;
if (! oForm.originalSubmit) {
oForm.originalSubmit = oForm.submit ;
oForm.submit = function() {
if (this.submitUpload) {
for (var i = 0 ; i < this.submitUpload.length ; i++) {
this.submitUpload[i]() ;
}
}
this.originalSubmit() ;
}
}
// 上传表单已装入完成
try {
parent.UploadLoaded();
}
catch(e){
}
</script>
</body>
</html>
<%
//System.out.println(sAction);
if (sAction!=null && sAction.equals("save"))
{
//调用SmartUpload实现文件上传功能
//上传成功之后,改变父窗口
//上载附件
String strFileName = null;
String strExtName = null;
String strTmp = null;
java.util.Date nowDate = new java.util.Date(System.currentTimeMillis());
%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<%
//************upload start****************************
mySmartUpload.initialize(pageContext);
//sContentPath = request.getContextPath() + sContentPath;
//mySmartUpload.setTotalMaxFileSize(lAllowSize);
//System.out.println("tttt" + sContentPath);
try {
mySmartUpload.upload();
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
strTmp = Long.toString(nowDate.getTime()); //获取当前时间
strFileName = myFile.getFileName();
sOriginalFileName = strFileName; //获取上载文件原始名称
strExtName = strFileName.substring(strFileName.lastIndexOf('.')); //获取上载文件的扩展名
sSaveFileName = strTmp + strExtName; //生成一个新文件名(当前时间)
sSaveFileName = sSaveFileName.toLowerCase();
sPathFileName = sContentPath + sSaveFileName;
// Save the files with its original names in a virtual path of the web server
//System.out.println("ccccccccc"+application.getRealPath(sContentPath));
myFile.saveAs(application.getRealPath(sContentPath) + "\\"+ sSaveFileName,mySmartUpload.SAVE_AUTO);
sSaveFileName = request.getContextPath() + "/" +sSaveFileName;
sPathFileName = request.getContextPath() + sPathFileName;
//System.out.println(sSaveFileName+"=============="+sPathFileName);
//myFile.saveAs("/temp/cms" + "\\"+ sSaveFileName,mySmartUpload.SAVE_AUTO);
}
}// end for
}// end try
catch(Exception e){
//将Exception对象中的错误信息打印到网页上来
System.out.println(e.toString());
java.io.StringWriter myout = new java.io.StringWriter();
e.printStackTrace(new java.io.PrintWriter(myout));
String trace = myout.toString();
out.print("<pre>"+trace+"</pre>");
String message = "<font color=\"#ff0000\">发生错误!</font>";
out.print(message);
}// end catch
%>
<SCRIPT LANGUAGE="JavaScript">
<!--
parent.UploadSaved("<%=sPathFileName%>");
var obj=parent.dialogArguments.dialogArguments;
if (!obj) obj=parent.dialogArguments;
try
{
obj.addUploadFile("<%=sOriginalFileName%>","<%=sSaveFileName%>","<%=sPathFileName%>");
}
catch(e){}
//-->
</SCRIPT>
<%
}
}%>
<%
jspWriter = out;
%>
<%!
private JspWriter jspWriter;
/*public String GetRndFileName(String sExt)
{
String strReturn;
strReturn = String.valueOf(System.currentTimeMillis()) + "." + sExt;
return strReturn;
}
//输出客户端脚本
public void OutScript(String str)
{
jspWriter.println("<script language=javascript>" + str + ";history.back()</script>");
}
public void OutScriptNoBack(String str)
{
jspWriter.println("<script language=javascript>" + str + "</script>");
}*/
/*public void DoSave()
{
//默认无组件上传类
//Call DoUpload_Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -