upload.jsp
上传用户:huijianzhu
上传日期:2009-11-25
资源大小:9825k
文件大小:15k
- <%@ 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("提示:nn请选择一个有效的文件,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
- sPathFileName = sContentPath + sSaveFileName;
- OutScript("parent.UploadSaved('" & sPathFileName & "');var obj=parent.dialogArguments.dialogArguments;if (!obj) obj=parent.dialogArguments;try{obj.addUploadFile('" & sOriginalFileName & "', '" & sSaveFileName & "', '" & sPathFileName & "');} catch(e){}");
- }*/
- /*自动获取远程文件
- public void DoRemote()
- {
- Dim sContent, i
- For i = 1 To Request.Form("eWebEditor_UploadText").Count
- sContent = sContent & Request.Form("eWebEditor_UploadText")(i)
- Next
- If sAllowExt <> "" Then
- sContent = ReplaceRemoteUrl(sContent, sAllowExt)
- End If
- Response.Write "<HTML><HEAD><TITLE>远程上传</TITLE><meta http-equiv='Content-Type' content='text/html; charset=gb2312'></head><body>" & _
- "<input type=hidden id=UploadText value=""" & inHTML(sContent) & """>" & _
- "</body></html>"
- Call OutScriptNoBack("parent.setHTML(UploadText.value);try{parent.addUploadFile('" & sOriginalFileName & "', '" & sSaveFileName & "', '" & sPathFileName & "');} catch(e){} parent.remoteUploadOK();")
- }*/
- /*
- public void DoUpload_Class()
- {
- On Error Resume Next
- Dim oUpload, oFile
- ' 建立上传对象
- Set oUpload = New upfile_class
- ' 取得上传数据,限制最大上传
- oUpload.GetData(nAllowSize*1024)
- If oUpload.Err > 0 Then
- Select Case oUpload.Err
- Case 1
- Call OutScript("parent.UploadError('请选择有效的上传文件!')")
- Case 2
- Call OutScript("parent.UploadError('你上传的文件总大小超出了最大限制(" & nAllowSize & "KB)!')")
- End Select
- Response.End
- End If
- Set oFile = oUpload.File("uploadfile")
- sFileExt = LCase(oFile.FileExt)
- Call CheckValidExt(sFileExt)
- sOriginalFileName = oFile.FileName
- sSaveFileName = GetRndFileName(sFileExt)
- oFile.SaveToFile Server.Mappath(sUploadDir & sSaveFileName)
-
- Set oFile = Nothing
- Set oUpload = Nothing
- }*/
- /*public String GetRndFileName(String sExt)
- {
- String strReturn;
- strReturn = String.valueOf(System.currentTimeMillis()) + "." + sExt;
- return strReturn;
- }
- //输出客户端脚本
- public void OutScript(String str)
- {
- out.println("<script language=javascript>" & str & ";history.back()</script>");
- }
- public void OutScriptNoBack(str)
- {
- out.println("<script language=javascript>" & str & "</script>");
- }
- */
- /*检测扩展名的有效性
- public void CheckValidExt(String sExt)
- {
- Dim b, i, aExt
- b = False
- aExt = Split(sAllowExt, "|")
- For i = 0 To UBound(aExt)
- If LCase(aExt(i)) = sExt Then
- b = True
- Exit For
- End If
- Next
- If b = False Then
- OutScript("parent.UploadError('提示:nn请选择一个有效的文件,n支持的格式有("+sAllowExt+")!')")
- Response.End
- End If
- }*/
- /*
- // 转为根路径格式
- Function RelativePath2RootPath(url)
- Dim sTempUrl
- sTempUrl = url
- If Left(sTempUrl, 1) = "/" Then
- RelativePath2RootPath = sTempUrl
- Exit Function
- End If
- Dim sWebEditorPath
- sWebEditorPath = Request.ServerVariables("SCRIPT_NAME")
- sWebEditorPath = Left(sWebEditorPath, InstrRev(sWebEditorPath, "/") - 1)
- Do While Left(sTempUrl, 3) = "../"
- sTempUrl = Mid(sTempUrl, 4)
- sWebEditorPath = Left(sWebEditorPath, InstrRev(sWebEditorPath, "/") - 1)
- Loop
- RelativePath2RootPath = sWebEditorPath & "/" & sTempUrl
- End Function
- */
- /*根路径转为带域名全路径格式
- Function RootPath2DomainPath(url)
- Dim sHost, sPort
- sHost = Split(Request.ServerVariables("SERVER_PROTOCOL"), "/")(0) & "://" & Request.ServerVariables("HTTP_HOST")
- sPort = Request.ServerVariables("SERVER_PORT")
- If sPort <> "80" Then
- sHost = sHost & ":" & sPort
- End If
- RootPath2DomainPath = sHost & url
- End Function*/
- //================================================
- //作 用:替换字符串中的远程文件为本地文件并保存远程文件
- //参 数:
- // sHTML : 要替换的字符串
- // sExt : 执行替换的扩展名
- //================================================
- /*Function ReplaceRemoteUrl(sHTML, sExt)
- Dim s_Content
- s_Content = sHTML
- If IsObjInstalled("Microsoft.XMLHTTP") = False then
- ReplaceRemoteUrl = s_Content
- Exit Function
- End If
-
- Dim re, RemoteFile, RemoteFileurl, SaveFileName, SaveFileType
- Set re = new RegExp
- re.IgnoreCase = True
- re.Global = True
- re.Pattern = "((http|https|ftp|rtsp|mms):(//|\\){1}(([A-Za-z0-9_-])+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(S*/)((S)+[.]{1}(" & sExt & ")))"
- Set RemoteFile = re.Execute(s_Content)
- Dim a_RemoteUrl(), n, i, bRepeat
- n = 0
- ' 转入无重复数据
- For Each RemoteFileurl in RemoteFile
- If n = 0 Then
- n = n + 1
- Redim a_RemoteUrl(n)
- a_RemoteUrl(n) = RemoteFileurl
- Else
- bRepeat = False
- For i = 1 To UBound(a_RemoteUrl)
- If UCase(RemoteFileurl) = UCase(a_RemoteUrl(i)) Then
- bRepeat = True
- Exit For
- End If
- Next
- If bRepeat = False Then
- n = n + 1
- Redim Preserve a_RemoteUrl(n)
- a_RemoteUrl(n) = RemoteFileurl
- End If
- End If
- Next
- ' 开始替换操作
- nFileNum = 0
- For i = 1 To n
- SaveFileType = Mid(a_RemoteUrl(i), InstrRev(a_RemoteUrl(i), ".") + 1)
- SaveFileName = GetRndFileName(SaveFileType)
- If SaveRemoteFile(SaveFileName, a_RemoteUrl(i)) = True Then
- nFileNum = nFileNum + 1
- If nFileNum > 0 Then
- sOriginalFileName = sOriginalFileName & "|"
- sSaveFileName = sSaveFileName & "|"
- sPathFileName = sPathFileName & "|"
- End If
- sOriginalFileName = sOriginalFileName & Mid(a_RemoteUrl(i), InstrRev(a_RemoteUrl(i), "/") + 1)
- sSaveFileName = sSaveFileName & SaveFileName
- sPathFileName = sPathFileName & sContentPath & SaveFileName
- s_Content = Replace(s_Content, a_RemoteUrl(i), sContentPath & SaveFileName, 1, -1, 1)
- End If
- Next
- ReplaceRemoteUrl = s_Content
- End Function
- */
- /*
- //================================================
- //作 用:保存远程的文件到本地
- //参 数:s_LocalFileName ------ 本地文件名
- // s_RemoteFileUrl ------ 远程文件URL
- //返回值:True ----成功
- // False ----失败
- //================================================
- Function SaveRemoteFile(s_LocalFileName, s_RemoteFileUrl)
- Dim Ads, Retrieval, GetRemoteData
- Dim bError
- bError = False
- SaveRemoteFile = False
- On Error Resume Next
- Set Retrieval = Server.CreateObject("Microsoft.XMLHTTP")
- With Retrieval
- .Open "Get", s_RemoteFileUrl, False, "", ""
- .Send
- GetRemoteData = .ResponseBody
- End With
- Set Retrieval = Nothing
- If LenB(GetRemoteData) > nAllowSize*1024 Then
- bError = True
- Else
- Set Ads = Server.CreateObject("Adodb.Stream")
- With Ads
- .Type = 1
- .Open
- .Write GetRemoteData
- .SaveToFile Server.MapPath(sUploadDir & s_LocalFileName), 2
- .Cancel()
- .Close()
- End With
- Set Ads=nothing
- End If
- If Err.Number = 0 And bError = False Then
- SaveRemoteFile = True
- Else
- Err.Clear
- End If
- End Function
- */
- //================================================
- //作 用:检查组件是否已经安装
- //参 数:strClassString ----组件名
- //返回值:True ----已经安装
- // False ----没有安装
- //================================================
- /*
- Function IsObjInstalled(strClassString)
- On Error Resume Next
- IsObjInstalled = False
- Err = 0
- Dim xTestObj
- Set xTestObj = Server.CreateObject(strClassString)
- If 0 = Err Then IsObjInstalled = True
- Set xTestObj = Nothing
- Err = 0
- End Function
- */
- %>