upload.jsp
上传用户:sdtxjx
上传日期:2022-07-09
资源大小:2937k
文件大小:15k
源码类别:

Jsp/Servlet

开发平台:

Java

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