upload.jsp
上传用户:zdly666
上传日期:2022-08-09
资源大小:6511k
文件大小:14k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

JavaScript

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