FileUploading.java
上传用户:u_thks
上传日期:2022-07-31
资源大小:1910k
文件大小:3k
源码类别:

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

开发平台:

Java

  1. /*
  2.  * Made In GamVan
  3.  */
  4. package com.gamvan.fileUpload;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import javax.servlet.http.HttpSession;
  12. import com.gamvan.tools.Arith;
  13. import com.gamvan.tools.TypeChange;
  14. public class FileUploading  extends HttpServlet {
  15.     /**
  16.      * 
  17.      */
  18.     private static final long serialVersionUID = 1L;
  19.     protected void service(HttpServletRequest req, HttpServletResponse rep) throws ServletException, IOException {
  20.         
  21.         rep.setCharacterEncoding("UTF-8");
  22.         rep.setContentType("text/html");
  23.         PrintWriter out = rep.getWriter();
  24.         out.print("<html><head><title>上传进度条 powered by GamVan.com</title>");
  25.         out.print("<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">");
  26.         out.print("</head>");
  27.         
  28.         HttpSession session = req.getSession();
  29.         String str = String.valueOf(session.getAttribute("fileSize"));
  30.         String str1 = String.valueOf(session.getAttribute("readsBytes"));
  31.         long fileSize = TypeChange.stringToLong(str);
  32.         long readsBytes = TypeChange.stringToLong(str1);
  33.         int i = 0; //上传进度
  34.         double d = TypeChange.longToDouble(fileSize);
  35.         double d2 = TypeChange.longToDouble(readsBytes);
  36.         i = TypeChange.doubleToInt(Arith.div(d2,d,2)*100);
  37.         int imgWidth = 0;
  38.         imgWidth = (i*3);
  39.         
  40.         if(fileSize==readsBytes && readsBytes>0){
  41.             session.removeAttribute("fileSize");
  42.             session.removeAttribute("readsBytes");
  43.             str = "上传完毕";
  44.             str1 = "上传完毕";
  45.         }else{
  46.             rep.setHeader("Refresh", "2");
  47.         }
  48.         
  49.         StringBuffer sb = new StringBuffer();
  50.         sb.append("<script language="javascript">n");
  51.         sb.append("function loading(){n");
  52.         
  53.         sb.append("if(!parent.document.layers&&!parent.document.all){n");
  54.             sb.append("return;n");
  55.         sb.append("}n");
  56.         
  57.         sb.append("if(document.layers){n");
  58.             sb.append("parent.document.layers.fileSize.document.write(""+ str +"");n");
  59.             sb.append("parent.document.layers.readsBytes.document.write(""+ str1 +"");n");
  60.             sb.append("parent.document.layers.readSchedule.document.write(""+ i +"%");n");
  61.             sb.append("parent.document.images.imgLoading.width="+imgWidth+"");
  62.         sb.append("}else if(document.all){n");
  63.             sb.append("parent.fileSize.innerHTML=""+ str +"";n");
  64.             sb.append("parent.readsBytes.innerHTML=""+ str1 +"";n");
  65.             sb.append("parent.readSchedule.innerHTML=""+ i +"%";n");
  66.             sb.append("parent.imgLoading.width="+imgWidth+"");
  67.             
  68.         sb.append("}n");  
  69.         sb.append("}n");
  70.         sb.append("</script>");
  71.         out.print(sb.toString());        
  72.         out.print("<body onload="loading();">");
  73.         out.print("</body></html>");
  74.         out.close();
  75.     } 
  76.     
  77.     
  78. }