upload.jsp
上传用户:lm2018
上传日期:2015-12-12
资源大小:30449k
文件大小:15k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. <%
  2. /** 名称:eWebEditor在线文本编辑器--国生修改版2007.5.3
  3. *   作者:国生
  4. *  日期:2007.5.3
  5. *   网址:http://www.gs008.com
  6. */
  7. %>
  8. <%@ page contentType="text/html; charset=GB2312" errorPage="uploadError.jsp"%>
  9. <%@ page import="java.sql.*"%>
  10. <%@ page import="java.io.*"%>
  11. <%@ page import="java.util.*"%>
  12. <%@ page import="java.util.regex.*"%>
  13. <%@ page import="org.ggs.editor.*"%>
  14. <jsp:useBean id="date" scope="page" class="org.ggs.editor.TimeStamp"/>
  15. <%!
  16. // 参数变量
  17. String sType, sStyleName;
  18. //' 设置变量
  19. String sAllowExt, sUploadDir,sBaseUrl,sContentPath;
  20. int  nAllowSize;
  21. //' 接口变量
  22. String sFileExt,sSaveFileName,sOriginalFileName,sPathFileName,FileName, nFileNum;
  23. String sAction;
  24. Connection connect = null;
  25. %>
  26. <%!
  27. /*' ============================================
  28. ' 去除Html格式,用于从数据库中取出值填入输入框时
  29. ' 注意:value="?"这边一定要用双引号
  30. ' ============================================*/
  31. public String inHTML(String str)
  32. {
  33. String sTemp;
  34. sTemp = str;
  35. if(sTemp.equals(""))
  36. {
  37. System.exit(0);
  38. }
  39. sTemp = sTemp.replaceAll("&", "&amp;");
  40. sTemp = sTemp.replaceAll("<", "&lt;");
  41. sTemp = sTemp.replaceAll(">", "&gt;");
  42. sTemp = sTemp.replaceAll(""", "&quot;");
  43. return sTemp;
  44. }
  45. //初始化上传限制数据
  46. public void InitUpload(String realpath){
  47. UploadWebHelper uw = new UploadWebHelper();
  48. uw.filename = realpath+"WEB-INF/style.xml";
  49. uw.getInstance();
  50. UploadBean bean = uw.InitPara();
  51. try{
  52. //sUploadDir = bean.getSuploaddir();
  53.         //System.out.println(sUploadDir);
  54.   Calendar calendar = Calendar.getInstance();
  55. //此处设置上传文件保存路径,注意路径要由ROOT开始=========================
  56. //修改此处需修改WEB-INF/Style.xml文件对应处<suploaddir>/UploadFile/</suploaddir>=========
  57. //修改此处需修改eWebEditor.jsp文件对应处=========www.ITstudy.cn=========
  58. String sUploadFilePath="/UploadFile/";
  59. // sUploadDir =realpath+ sUploadFilePath +calendar.get(Calendar.YEAR) + "/"+ (calendar.get(Calendar.MONTH)+1) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + "/";
  60. sUploadDir =realpath+ sUploadFilePath  + "/";
  61.  //sUploadDir = realpath+"articlemanage/article/";
  62. //sUploadDir = bean.getSuploaddir();
  63. //sUploadDir = realpatha;
  64. //System.out.println(sUploadDir);
  65. if(sType.equalsIgnoreCase("remote"))
  66. {
  67. sAllowExt =  bean.getSremoteext();
  68.                         sAllowExt = sAllowExt + "|" + sAllowExt.toUpperCase();
  69. nAllowSize =  Integer.parseInt(bean.getSremotesize()) ;
  70.                         System.out.println(sAllowExt+nAllowSize);
  71. }
  72.                 else if(sType.equalsIgnoreCase("file"))
  73.                {
  74. sAllowExt = bean.getSfileext();
  75.                         sAllowExt = sAllowExt + "|" + sAllowExt.toUpperCase();
  76. nAllowSize = Integer.parseInt(bean.getSfilesize());
  77.                         System.out.println(sAllowExt+nAllowSize);
  78.                }
  79.                else if(sType.equalsIgnoreCase("media"))
  80. {
  81. sAllowExt =  bean.getSmediaext();
  82.                         sAllowExt = sAllowExt + "|" + sAllowExt.toUpperCase();
  83. nAllowSize = Integer.parseInt(bean.getSmediasize());
  84.                         System.out.println(sAllowExt+nAllowSize);
  85. }
  86.                 else if(sType.equalsIgnoreCase("flash"))
  87.                 {
  88. sAllowExt =  bean.getSflashext();
  89.                         sAllowExt = sAllowExt + "|" + sAllowExt.toUpperCase();
  90. nAllowSize = Integer.parseInt(bean.getSflashsize());
  91.                         System.out.println(sAllowExt+nAllowSize);
  92.                 }
  93. else
  94.                 {
  95. sAllowExt =  bean.getSimageext();
  96.                         sAllowExt = sAllowExt + "|" + sAllowExt.toUpperCase();
  97. nAllowSize = Integer.parseInt(bean.getSimagesize());
  98.                         System.out.println(sAllowExt+nAllowSize);
  99.                 }
  100. }
  101. catch(Exception e){
  102.         }
  103. }
  104. %>
  105. <%
  106. //设置类型
  107. sType=request.getParameter("type");
  108. if(sType==null)
  109. {
  110. sType="image";
  111. }
  112. else
  113. sType=request.getParameter("type").trim();
  114. //设置样式
  115. sStyleName=request.getParameter("style");
  116. if (sStyleName==null)
  117. {
  118.   sStyleName="standard";
  119. }
  120. else
  121. sStyleName=request.getParameter("style").trim();
  122. //设置动作
  123. sAction=request.getParameter("action");
  124. if(sAction==null)
  125. {
  126.   sAction="sun";
  127. }
  128. else
  129. sAction=request.getParameter("action").trim();
  130. %>
  131. <%
  132. //初始化上传变量
  133. InitUpload(config.getServletContext().getRealPath("/"));
  134. //断开数据库连接
  135. //sAction = UCase(Trim(Request.QueryString("action"))
  136. if(sAction.equalsIgnoreCase("remote"))
  137. {    //远程自动获取
  138. String sContent;
  139. String RemoteFileurl=null;
  140. String Protocol,sUrl;
  141. int Port;
  142. String LocalFileurl=null;
  143. String SrcFileurl=null;
  144. String SaveFileName=null;
  145. sContent=request.getParameter("eWebEditor_UploadText");
  146.     if(sContent==null)
  147. {
  148. sContent="sunshanfeng";
  149. }
  150. else
  151. sContent=new String(request.getParameter("eWebEditor_UploadText").getBytes("iso8859-1"));
  152. System.out.println();
  153. System.out.println();
  154. System.out.println();
  155. System.out.println();
  156.     System.out.println("替换前的html标记为:"+"n"+sContent);
  157. if(sAllowExt!="")
  158. {
  159. Pattern pRemoteFileurl = Pattern.compile("((http|https|ftp|rtsp|mms):(//|\\){1}(([A-Za-z0-9_-])+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(\S*/)((\S)+[.]{1}("+sAllowExt+")))");//取得网页上URL的正则表达式
  160.     Matcher mRemoteFileurl = pRemoteFileurl.matcher(sContent);//对传入的字符串进行匹配
  161. Protocol=request.getProtocol();//取得通讯的协议
  162. String ProtocolA[]=Protocol.split("/");//取得协议前面的字母,如HTTP/1.1,变为"HTTP","1.1"
  163. sUrl = ProtocolA[0]+"://"+request.getServerName();//取得本地URL路径,如http://localhost
  164. //ProtocolA[]=null;
  165. Port=request.getServerPort();//取得端口值
  166. if(Port!=80)
  167. {//查看端口是否为80,如果不是还需要在联接上加上端口
  168.      sUrl=sUrl+":"+Port;
  169. }
  170. String context=request.getContextPath();
  171. sUrl=sUrl+context+"/"+sUploadDir;
  172. System.out.println(sUrl);
  173. StringBuffer sb=new StringBuffer();
  174. boolean result=mRemoteFileurl.find();
  175. int i=0;
  176.        while(result)
  177. {
  178.              i++;
  179.              RemoteFileurl=mRemoteFileurl.group(0);
  180.  System.out.println();
  181.  System.out.println();
  182.  System.out.println();
  183.  System.out.println();
  184.  System.out.println("需要替换的远程连接:"+"n"+RemoteFileurl);
  185.  sOriginalFileName=RemoteFileurl.substring(RemoteFileurl.lastIndexOf("/"));
  186.  Pattern pFileType=Pattern.compile("[.]{1}("+sAllowExt+")");//二次匹配取得文件的类型
  187.  Matcher mFileType=pFileType.matcher(RemoteFileurl);
  188.  while(mFileType.find())
  189. {
  190.  String SaveFileType=mFileType.group();
  191.  LocalFileurl=sUploadDir+(String)date.Time_Stamp()+i+SaveFileType;//文件的路径,以时间戳命名
  192. }
  193.    String LoadFile=sUploadDir.substring(0,sUploadDir.length()-1); SaveFileName=config.getServletContext().getRealPath("/")+LoadFile+"\"+LocalFileurl.substring(LocalFileurl.lastIndexOf("/")+1);
  194.    System.out.println();
  195.    System.out.println();
  196.    System.out.println();
  197. System.out.println("远程文件保存的路径和文件名:"+"n"+SaveFileName);
  198.                 sSaveFileName=LocalFileurl.substring(LocalFileurl.lastIndexOf("/"));
  199.                 RemotePic Down=new RemotePic();
  200. Down.picurl=RemoteFileurl;
  201. Down.savepath=SaveFileName;
  202.              if (Down.download())//如果上载保存成功,则更换html标记里的文件路径
  203. {
  204.  mRemoteFileurl.appendReplacement(sb,LocalFileurl);//替换路径
  205. }
  206.              result=mRemoteFileurl.find();
  207. }
  208. mRemoteFileurl.appendTail(sb);
  209. sContent=sb.toString();
  210. }
  211. sContent=inHTML(sContent);
  212. System.out.println();
  213. System.out.println();
  214. System.out.println();
  215. System.out.print("替换后的html标记:"+"n"+sContent);
  216. out.println("<HTML><HEAD><TITLE>远程上传</TITLE><meta http-equiv='Content-Type' content='text/html; charset=gb2312'></head><body>");
  217. out.print("<input type=hidden id=UploadText value="");
  218. out.print(sContent);
  219. out.println("">");
  220. out.println("</body></html>");
  221. out.println("<script language=javascript>");
  222. out.print("parent.setHTML(UploadText.value);try{parent.addUploadFile('");//为什么只取一半的值?且只取复制网页插入位置之前的值?
  223. out.print(sOriginalFileName);
  224. out.print("', '");
  225. out.print(sSaveFileName);
  226. out.print("', '");
  227. out.print(SaveFileName);
  228. out.println("');} catch(e){} parent.remoteUploadOK();");
  229. out.println("</script>");
  230.   //DoRemote();
  231. }
  232. else if(sAction.equalsIgnoreCase("save"))
  233. {
  234.   //显示上传菜单
  235.  out.println("<HTML>");
  236.         out.println("<HEAD>");
  237.         out.println("<TITLE>文件上传</TITLE>");
  238.         out.println("<meta http-equiv="Content-Type" content="text/html; charset=gb2312">");
  239.         out.println("<style type="text/css">");
  240.         out.println("body, a, table, div, span, td, th, input, select{font:9pt;font-family: "宋体", Verdana, Arial, Helvetica, sans-serif;}");
  241.         out.println("body {padding:0px;margin:0px}");
  242.         out.println("</style>");
  243.         out.println("<script language="JavaScript" src="dialog/dialog.js">");
  244.         out.println("</script>");
  245.         out.println("</head>");
  246.         out.println("<body bgcolor=menu>");
  247.         out.print("<form action="?action=save&type=");//注意此处为什么不用println()
  248.         out.print(sType);
  249.         out.print("&style=");
  250.         out.print(sStyleName);
  251.         out.println("" method=post name=myform enctype="multipart/form-data">");
  252.         out.println("<input type=file name=uploadfile size=1 style="width:100%" onchange="originalfile.value=this.value">");
  253.         out.println("<input type=hidden name=originalfile value="">");
  254.         out.println("</form>");
  255.         out.println("<script language=javascript>");
  256.         out.print("var sAllowExt = "");
  257.         out.print(sAllowExt);
  258.         out.println("";");
  259.         out.println("// 检测上传表单");
  260.         out.println("function CheckUploadForm() {");
  261.         out.println("if (!IsExt(document.myform.uploadfile.value,sAllowExt)){");
  262.         out.println("parent.UploadError("提示:\n\n请选择一个有效的文件,\n支持的格式有("+sAllowExt+")!");");
  263.         out.println("return false;");
  264.         out.println("}");
  265.         out.println("return true");
  266.         out.println("}");
  267.         out.println("// 提交事件加入检测表单");
  268.         out.println("var oForm = document.myform;");
  269.         out.println("oForm.attachEvent("onsubmit", CheckUploadForm) ;");
  270.         out.println("if (! oForm.submitUpload) oForm.submitUpload = new Array() ;");
  271.         out.println("oForm.submitUpload[oForm.submitUpload.length] = CheckUploadForm ;");
  272.         out.println("if (! oForm.originalSubmit) {");
  273.         out.println("oForm.originalSubmit = oForm.submit ;");
  274.         out.println("oForm.submit = function() {");
  275.         out.println("if (this.submitUpload) {");
  276.         out.println("for (var i = 0 ; i < this.submitUpload.length ; i++) {");
  277.         out.println("this.submitUpload[i]() ;");
  278.         out.println(" }");
  279.         out.println(" }");
  280.         out.println(" this.originalSubmit() ;");
  281.         out.println(" }");
  282.         out.println("}");
  283.         out.println("// 上传表单已装入完成");
  284.         out.println("try {");
  285.         out.println(" parent.UploadLoaded();");
  286.         out.println("}");
  287.         out.println("catch(e){");
  288.         out.println("}");
  289.         out.println("</script>");
  290.         out.println("</body>");
  291.         out.println("</html>");
  292.   //存文件
  293.   //DoSave();
  294.     SmartUpload up = new SmartUpload();
  295.    //初始化上传组件
  296.      up.initialize(pageContext);
  297.    //设置上传文件大小
  298.      up.setMaxFileSize(nAllowSize*1024);
  299.    //设置上传文件类型
  300.      String setExt=sAllowExt.replace('|',',');
  301.      up.setAllowedFilesList(setExt);
  302. // Upload
  303. up.upload();
  304. // Select each file
  305. for (int i=0;i<up.getFiles().getCount();i++){
  306. // Retreive the current file
  307. org.ggs.editor.File myFile = up.getFiles().getFile(i);
  308. if (!myFile.isMissing()) {
  309. FileName=(String)date.Time_Stamp();
  310. sOriginalFileName=myFile.getFileName();
  311. myFile.saveAs("/"+sUploadDir+FileName+"."+myFile.getFileExt());
  312. sSaveFileName=FileName+"."+myFile.getFileExt();
  313. sPathFileName=sUploadDir+sSaveFileName;
  314. System.out.println(sPathFileName);
  315. }
  316. }
  317.    out.println("<script language=javascript>");
  318. out.print("parent.UploadSaved('");
  319. out.print(sSaveFileName);
  320. out.print("');var obj=parent.dialogArguments.dialogArguments;if (!obj) obj=parent.dialogArguments;try{obj.addUploadFile('");
  321. out.print(sOriginalFileName);
  322. out.print("', '");
  323. out.print(sSaveFileName);
  324. out.print("', '");
  325. out.print(sPathFileName);
  326. out.print("');} catch(e){}");
  327. out.println(";history.back()</script>");
  328. }
  329. else
  330. {
  331.   //显示上传表单
  332.          out.println("<HTML>");
  333.         out.println("<HEAD>");
  334.         out.println("<TITLE>文件上传</TITLE>");
  335.         out.println("<meta http-equiv="Content-Type" content="text/html; charset=gb2312">");
  336.         out.println("<style type="text/css">");
  337.         out.println("body, a, table, div, span, td, th, input, select{font:9pt;font-family: "宋体", Verdana, Arial, Helvetica, sans-serif;}");
  338.         out.println("body {padding:0px;margin:0px}");
  339.         out.println("</style>");
  340.         out.println("<script language="JavaScript" src="dialog/dialog.js">");
  341.         out.println("</script>");
  342.         out.println("</head>");
  343.         out.println("<body bgcolor=menu>");
  344.         out.print("<form action="?action=save&type=");
  345.         out.print(sType);
  346.         out.print("&style=");
  347.         out.print(sStyleName);
  348.         out.println("" method=post name=myform enctype="multipart/form-data">");
  349.         out.println("<input type=file name=uploadfile size=1 style="width:100%" onchange="originalfile.value=this.value">");
  350.         out.println("<input type=hidden name=originalfile value="">");
  351.         out.println("</form>");
  352.         out.println("<script language=javascript>");
  353.         out.print("var sAllowExt = "");
  354.         out.print(sAllowExt);
  355.         out.println("";");
  356.         out.println("// 检测上传表单");
  357.         out.println("function CheckUploadForm() {");
  358.         out.println(" if (!IsExt(document.myform.uploadfile.value,sAllowExt)){");
  359.         out.println(" parent.UploadError("提示:\n\n请选择一个有效的文件,\n支持的格式有("+sAllowExt+")!");");
  360.         out.println(" return false;");
  361.         out.println(" }");
  362.         out.println(" return true");
  363.         out.println("}");
  364.         out.println("// 提交事件加入检测表单");
  365.         out.println("var oForm = document.myform ;");
  366.         out.println("oForm.attachEvent("onsubmit", CheckUploadForm) ;");
  367.         out.println("if (! oForm.submitUpload) oForm.submitUpload = new Array() ;");
  368.         out.println("oForm.submitUpload[oForm.submitUpload.length] = CheckUploadForm ;");
  369.         out.println("if (! oForm.originalSubmit) {");
  370.         out.println(" oForm.originalSubmit = oForm.submit ;");
  371.         out.println(" oForm.submit = function() {");
  372.         out.println(" if (this.submitUpload) {");
  373.         out.println(" for (var i = 0 ; i < this.submitUpload.length ; i++) {");
  374.         out.println(" this.submitUpload[i]() ;");
  375.         out.println(" }");
  376.         out.println(" }");
  377.         out.println(" this.originalSubmit() ;");
  378.         out.println(" }");
  379.         out.println("}");
  380.         out.println("// 上传表单已装入完成");
  381.         out.println("try {");
  382.         out.println(" parent.UploadLoaded();");
  383.         out.println("}");
  384.         out.println("catch(e){");
  385.         out.println("}");
  386.         out.println("</script>");
  387.         out.println("</body>");
  388.         out.println("</html>");
  389.         //out.println("123");
  390. }
  391. %>