最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年06月03日 10:38:07   投稿:jingxian  
下面小編就為大家?guī)硪黄趂ileUpload文件上傳帶進(jìn)度條效果的實(shí)例(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

文件上傳過程中,如果我們能看到進(jìn)度條會更好,實(shí)現(xiàn)思路是服務(wù)器端用監(jiān)聽器實(shí)時監(jiān)聽進(jìn)度并存入session,客戶端異步請求服務(wù)器端獲得上傳進(jìn)度,并進(jìn)行效果渲染。

效果圖:

服務(wù)器端servlet:

public class UploadServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    //取出監(jiān)聽器MyProgress在session中保存的進(jìn)度信息
    String progress=(String) req.getSession().getAttribute("progress");
    //響應(yīng)
    resp.getWriter().print(progress);
    //清除session中保存的數(shù)據(jù)
//    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 {
      List<FileItem> 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("文件上傳發(fā)生錯誤!");
      e.printStackTrace();
    }
  }
}

服務(wù)器端監(jiān)聽器:

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) {
    //將數(shù)據(jù)進(jìn)行格式化
    //已讀取數(shù)據(jù)由字節(jié)轉(zhuǎn)換為M
    double readM=pBytesRead/1024.0/1024.0;
    //已讀取數(shù)據(jù)由字節(jié)轉(zhuǎn)換為M
    double totalM=pContentLength/1024.0/1024.0;
    //已讀取百分百
    double percent=readM/totalM;
    
    //格式化數(shù)據(jù)
    //已讀取
    String readf=dataFormat(pBytesRead);
    //總大小
    String totalf=dataFormat(pContentLength);
    //進(jìn)度百分百
    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);
  }
  /**
   * 格式化讀取數(shù)據(jù)的顯示
   * @param data要格式化的數(shù)據(jù) 單位byte
   * @return 格式化后的數(shù)據(jù),如果小于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>帶進(jìn)度條的文件上傳效果</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("上傳進(jìn)度:100%");
             //更新進(jìn)度條
            $("#progress").width("100%");
          }else{//正在上傳
            //更新進(jìn)度信息
            $("#proInfo").text("上傳進(jìn)度:"+data);
            //更新進(jìn)度條
            $("#progress").width(data);
          }
        });
      },200);
    }
    
  </script>
 </head>
 
 <body>
   <iframe name="aa" style="display: none;"></iframe>
  <h2>帶進(jìn)度條的文件上傳效果</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">上傳進(jìn)度:0%</span>
  </form>
 </body>
</html>

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

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

相關(guān)文章

  • 利用ajax提交form表單到數(shù)據(jù)庫詳解(無刷新)

    利用ajax提交form表單到數(shù)據(jù)庫詳解(無刷新)

    這篇文章主要給大家介紹了關(guān)于利用ajax提交form表單到數(shù)據(jù)庫(無刷新)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-02-02
  • Ajax初試之讀取數(shù)據(jù)篇

    Ajax初試之讀取數(shù)據(jù)篇

    上次我們講了"ajax開始準(zhǔn)備篇",做好了基本的ajax準(zhǔn)備工作以后.我們開始牛刀小試一下:ajax初試之讀取數(shù)據(jù)篇.是的,今天我們要實(shí)現(xiàn)的效果是.在不刷新網(wǎng)頁的情況下讀取并顯示服務(wù)端的數(shù)據(jù).
    2010-08-08
  • Ajax實(shí)現(xiàn)省市縣三級聯(lián)動

    Ajax實(shí)現(xiàn)省市縣三級聯(lián)動

    這篇文章主要為大家詳細(xì)介紹了Ajax實(shí)現(xiàn)省市縣三級聯(lián)動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • 解決ajax提交到后臺數(shù)據(jù)成功但返回不走success而走的error問題

    解決ajax提交到后臺數(shù)據(jù)成功但返回不走success而走的error問題

    今天小編就為大家分享一篇解決ajax提交到后臺數(shù)據(jù)成功但返回不走success而走的error問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • ajax同步驗(yàn)證單號是否存在的方法

    ajax同步驗(yàn)證單號是否存在的方法

    這篇文章主要介紹了ajax同步驗(yàn)證單號是否存在的方法,涉及基于ajax的數(shù)據(jù)交互相關(guān)操作技巧,需要的朋友可以參考下
    2016-08-08
  • ajax 服務(wù)器文本框自動填值

    ajax 服務(wù)器文本框自動填值

    最近二天。項(xiàng)目做完了。閑著沒事做就自己寫了一點(diǎn)東西。在寫的過程中。發(fā)現(xiàn)利用服務(wù)器的文本框去查找用戶的相關(guān)信息的時刻總要去刷新頁面。
    2009-06-06
  • ajax中設(shè)置contentType:

    ajax中設(shè)置contentType: "application/json"的作用

    這篇文章主要介紹了ajax中設(shè)置contentType: “application/json”的作用,需要的朋友可以參考下
    2018-04-04
  • Ajax實(shí)現(xiàn)頁面無刷新留言效果

    Ajax實(shí)現(xiàn)頁面無刷新留言效果

    這篇文章主要為大家詳細(xì)介紹了Ajax實(shí)現(xiàn)頁面無刷新留言效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • 用AJAX技術(shù)實(shí)現(xiàn)在自己Blog上聚合并顯示朋友Blog的最新文章

    用AJAX技術(shù)實(shí)現(xiàn)在自己Blog上聚合并顯示朋友Blog的最新文章

    在自己Blog上聚合并顯示朋友Blog的最新文章,這樣方便自己及時了解朋友的消息,另外,也方便訪問者找到和本Blog相關(guān)的blog和文章
    2014-05-05
  • Ajax跨域登錄請求未攜帶cookie錯誤解決

    Ajax跨域登錄請求未攜帶cookie錯誤解決

    這篇文章主要為大家介紹了Ajax跨域登錄請求未攜帶cookie錯誤解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10

最新評論

六安市| 四川省| 松溪县| 浑源县| 河源市| 正镶白旗| 乐至县| 吴江市| 怀来县| 宁国市| 东乡族自治县| 兴山县| 江永县| 镇远县| 长顺县| 温泉县| 伊春市| 襄汾县| 车致| 五台县| 昂仁县| 大足县| 凌云县| 双峰县| 京山县| 海盐县| 林甸县| 彰化县| 收藏| 宝兴县| 长寿区| 苗栗市| 阿克| 揭阳市| 平乐县| 县级市| 泽库县| 太谷县| 柯坪县| 垦利县| 丁青县|