婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 基于fileUpload文件上傳帶進度條效果的實例(必看)

基于fileUpload文件上傳帶進度條效果的實例(必看)

熱門標簽:立陶宛地圖標注 地圖標注推銷坑人 中國地圖標注不明確情況介紹表 怎樣在地圖標注文字 上海企業外呼系統價錢 大眾點評400電話怎么申請 東平縣地圖標注app 河間市地圖標注app 電銷機器人 長春

文件上傳過程中,如果我們能看到進度條會更好,實現思路是服務器端用監聽器實時監聽進度并存入session,客戶端異步請求服務器端獲得上傳進度,并進行效果渲染。

效果圖:

服務器端servlet:

public class UploadServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    //取出監聽器MyProgress在session中保存的進度信息
    String progress=(String) req.getSession().getAttribute("progress");
    //響應
    resp.getWriter().print(progress);
    //清除session中保存的數據
//    req.getSession().removeAttribute("progress");
  }
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    req.setCharacterEncoding("UTF-8");
    DiskFileItemFactory factory=new DiskFileItemFactory();
    ServletFileUpload upload=new ServletFileUpload(factory);
    upload.setProgressListener(new MyProgressListener(req));
    try {
      ListFileItem> list = upload.parseRequest(req);
      for (FileItem fileItem : list) {
        if (fileItem.isFormField()) {//普通表單
        }else{//上傳文件
          String path=req.getRealPath("uploads");
          String fileName=fileItem.getName();
          File file=new File(path, fileName);
          fileItem.write(file);
          System.out.println("成功上傳文件:"+fileName);
        }
      }
    } catch (Exception e) {
      System.out.println("文件上傳發生錯誤!");
      e.printStackTrace();
    }
  }
}

服務器端監聽器:

public class MyProgressListener implements ProgressListener {
  private HttpSession session;
  public MyProgressListener(HttpServletRequest request){
    session = request.getSession();
  }
  @Override
  public void update(long pBytesRead, long pContentLength, int pItems) {
    //將數據進行格式化
    //已讀取數據由字節轉換為M
    double readM=pBytesRead/1024.0/1024.0;
    //已讀取數據由字節轉換為M
    double totalM=pContentLength/1024.0/1024.0;
    //已讀取百分百
    double percent=readM/totalM;
    
    //格式化數據
    //已讀取
    String readf=dataFormat(pBytesRead);
    //總大小
    String totalf=dataFormat(pContentLength);
    //進度百分百
    NumberFormat format=NumberFormat.getPercentInstance();
    String progress=format.format(percent);
    
    //將信息存入session
    session.setAttribute("progress", progress);
    
    //打印消息到控制臺
    System.out.println("pBytesRead===>"+pBytesRead);
    System.out.println("pContentLength==>"+pContentLength);
    System.out.println("pItems===>"+pItems);
    System.out.println("readf--->"+readf);
    System.out.println("totalf--->"+totalf);
    System.out.println("progress--->"+progress);
  }
  /**
   * 格式化讀取數據的顯示
   * @param data要格式化的數據 單位byte
   * @return 格式化后的數據,如果小于1M顯示單位為KB,如果大于1M顯示單位為M
   */
  public String dataFormat(double data){
    String formdata="";
    if (data>=1024*1024) {//大于等于1M
      formdata=Double.toString(data/1024/1024)+"M";
    }else if(data>=1024){//大于等于1KB
      formdata=Double.toString(data/1024)+"KB";
    }else{//小于1KB
      formdata=Double.toString(data)+"byte";
    }
    return formdata.substring(0, formdata.indexOf(".")+2);
  }

}

客戶端:

html>
 head>
  base href="%=basePath%>" rel="external nofollow" >
  
  title>帶進度條的文件上傳效果/title>
  meta http-equiv="pragma" content="no-cache">
  meta http-equiv="cache-control" content="no-cache">
  meta http-equiv="expires" content="0">  
  meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  meta http-equiv="description" content="This is my page">
  style type="text/css">
    #progressBar{width: 300px;height: 20px;border: 1px #EEE solid;}
    #progress{width: 0%;height: 20px;background-color: lime;}
  /style>
  script type="text/javascript" src="js/jquery-1.4.2.js">/script>
  script type="text/javascript">
    function upload(){
      $("#f1").submit();
      var pro=null;
      pro=setInterval(function(){
        $.get("UploadServlet","",function(data){
          if(data=='100%'){
            clearInterval(pro);
            $("#proInfo").text("上傳進度:100%");
             //更新進度條
            $("#progress").width("100%");
          }else{//正在上傳
            //更新進度信息
            $("#proInfo").text("上傳進度:"+data);
            //更新進度條
            $("#progress").width(data);
          }
        });
      },200);
    }
    
  /script>
 /head>
 
 body>
   iframe name="aa" style="display: none;">/iframe>
  h2>帶進度條的文件上傳效果/h2>
  form target="aa" id="f1" action="UploadServlet" method="post" enctype="multipart/form-data">
    文件:input name="file" type="file">
    input type="button" value="上傳" onclick="upload();">
    div id="progressBar">
      div id="progress">/div>
    /div>
    span id="proInfo">上傳進度:0%/span>
  /form>
 /body>
/html>

說明:為了讓上傳后該頁面不跳轉,我們可以讓表單跳轉至一個隱藏的iframe。

以上這篇基于fileUpload文件上傳帶進度條效果的實例(必看)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • jQuery監聽文件上傳實現進度條效果的方法
  • jQuery實現文件上傳進度條特效
  • 基于Ajax技術實現文件上傳帶進度條
  • Jquery Uploadify多文件上傳帶進度條且傳遞自己的參數

標簽:本溪 四川 營口 內江 遼寧 益陽 銅川 玉樹

巨人網絡通訊聲明:本文標題《基于fileUpload文件上傳帶進度條效果的實例(必看)》,本文關鍵詞  基于,fileUpload,文件,上傳,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《基于fileUpload文件上傳帶進度條效果的實例(必看)》相關的同類信息!
  • 本頁收集關于基于fileUpload文件上傳帶進度條效果的實例(必看)的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 芦山县| 富源县| 平舆县| 富源县| 阿克苏市| 社会| 永善县| 张掖市| 纳雍县| 饶河县| 昌都县| 蚌埠市| 香格里拉县| 垫江县| 阜阳市| 永平县| 东城区| 桂东县| 嘉兴市| 灵石县| 油尖旺区| 新乡市| 定州市| 阿拉善右旗| 加查县| 鄂伦春自治旗| 连云港市| 台江县| 鄯善县| 兰州市| 将乐县| 托里县| 大同县| 雅江县| 治县。| 绥阳县| 宜城市| 政和县| 崇左市| 隆尧县| 孟连|