全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

struts2实现文件上传显示进度条效果

一. struts2读取进度原理分析(作为草稿存了好久,刚刚发布出来......)

1. 在strut2中控制文件上传信息的类是实现MultiPartRequest接口的JakartaMultiPartRequest

其实第一次看到源文件时我打了个退堂鼓,因为觉得内容太长了,不想看。冷静下来将思路理顺,将分开的各个方法还原到一个方方中中,发现还是很好理解的:

@Override
 public void parse(HttpServletRequest request, String saveDir)
   throws IOException {
  setLocale(request);
     //规定了File文件的格式(如文件名必须是xxFileName,文件类型xxContentType),并定义了File的保存路径                 DiskFileItemFactory factory = new DiskFileItemFactory(); 
  ServletFileUpload upload = new ServletFileUpload(factory);//处理文件上传的servlet
  upload.setProgressListener(new FileUploadProgressListener(request)); //为文件上传添加监听  factory.setSizeThreshold(0); //if (saveDir != null
   factory.setRepository(new File(saveDir));//临时路径
  }
  try {
   upload.setSizeMax(maxSize);
   List items = upload.parseRequest(createRequestContext(request)); //获取所有请求
   for (Object obItem : items) {
    FileItem item = (FileItem) obItem; //获取每个请求的文件
    if (LOG.isDebugEnabled()) {
     LOG.debug("Found item" + item.getFieldName());
    }
    if (item.isFormField()) { //普通表单提交
     LOG.debug("Item is a normal form field");
     List<String> values;
     if (params.get(item.getFieldName()) != null) {
      values = params.get(item.getFieldName());
     } else {
      values = new ArrayList<String>();
     }
     String charset = request.getCharacterEncoding();
     if (charset != null) {
      values.add(item.getString(charset));
     } else {
      values.add(item.getString());
     }
     params.put(item.getFieldName(), values);
    } else { //文件上传请求
     LOG.debug("Item is a file upload");
     if (item.getName() == null
       || item.getName().trim().length() <= 0) {
      LOG.debug("No file has been uploded for the filed:"
        + item.getFieldName());
      continue;
     }

     List<FileItem> values;
     if (files.get(item.getFieldName()) != null) {
      values = files.get(item.getFieldName());
     } else {
      values = new ArrayList<FileItem>();
     }
     values.add(item);
     files.put(item.getFieldName(), values);
    }
   }

  } catch (FileUploadBase.SizeLimitExceededException e) {
   System.out.println("错误1:" + e);
   if (LOG.isWarnEnabled()) {
    LOG.warn("Request exceeded size limit!", e);
   }
   String errorMessage = buildErrorMessage(e, new Object[]{e.getPermittedSize(), e.getActualSize()});
   if (!errors.contains(errorMessage)) {
    errors.add(errorMessage);
   }
  } catch (Exception e) {
   System.out.println("错误1:" + e);
   if (LOG.isWarnEnabled()) {
    LOG.warn("Unable to parse request", e);
   }
   String errorMessage = buildErrorMessage(e, new Object[]{});
   if (!errors.contains(errorMessage)) {
    errors.add(errorMessage);
   }
  }
 }

2.  文件上传监听文件FileUploadProgressListener.java

public class FileUploadProgressListener implements ProgressListener {
  private final HttpSession session;
  private final DecimalFormat format = new DecimalFormat("#00.0");

  public FileUploadProgressListener(HttpServletRequest request) {
    session = request.getSession();
    FileUploadStatus status = new FileUploadStatus();
    session.setAttribute("uploadStatus", status);
  }

  @Override
  public void update(long pBytesRead, long pContentLength, int pItems) {
    FileUploadStatus uploadStatus = (FileUploadStatus) session.getAttribute("uploadStatus");
    Double uploadRate = (double) (pBytesRead * 100 / pContentLength);
    uploadStatus.setUploadRate(Double.valueOf(format.format(uploadRate)));
    uploadStatus.setReadedBytes(pBytesRead / 1024);
    uploadStatus.setTotalBytes(pContentLength / 1024);
    uploadStatus.setCurrentItems(pItems);
  }
}

3. 添加状态文件:FileUploadStatus.java

public class FileUploadStatus {
 private Double uploadRate = 0.0;
 private Long readedBytes = 0L;
 private Long totalBytes = 0L;
 private int currentItems = 0;
 private Long uploadSpeed = 0L;
 private Long startTime = System.currentTimeMillis();
 private Long readedTimes = 0L;
 private Long totalTimes = 0L;
 // "-1" 错误 "0" 正常 "1" 完成
 private String error = "0";

 ...
  setter getter方法
 ...  
}

4. Action类(如果是多文件上传,则将File   FileName   ContentType定义成数组形式即可)

/**
 * 利用io流上传文件
 */
public class FileStreamUploadAction extends ActionSupport {
 /**
  * serialVersionUID作用: ---相当于类的身份证。 序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。
  * 有两种生成方式: 一个是默认的1L,比如:private static final long serialVersionUID = 1L;
  * 一个是根据类名、接口名、成员方法及属性等来生成一个64位的哈希字段,比如: private static final long
  * serialVersionUID = xxxxL;
  */
 private static final long serialVersionUID = 1L;
 private File image;
 private String imageFileName;
 private String imageContentType;
 private String message;
 public String uploadFile() {
  FileInputStream in = null;
  FileOutputStream out = null;
  System.out.println("文件名:" + imageFileName);
  try {
   this.setNewFileName(imageFileName);
   String realPath = ServletActionContext.getServletContext()
     .getRealPath("/file");
   File filePath = new File(realPath);
   if (!filePath.exists()) { // 如果保存的路径不存在则创建
    filePath.mkdir();
   }
   if (image == null) {
    message = "上传文件为空";
    System.out.println(message);
   } else {
    File saveFile = new File(filePath, this.getNewFileName());
    out = new FileOutputStream(saveFile);
   }
   in = new FileInputStream(image);
   byte[] byt = new byte[1024];
   int length = 0;
   while ((length = in.read(byt)) > 0) {
    out.write(byt, 0, length);
    out.flush();
   }
   message = "上传成功";
   System.out.println(message);
  } catch (FileNotFoundException e) {
   message = "找不到文件!";
   e.printStackTrace();
  } catch (IOException e) {
   message = "文件读取失败!";
   e.printStackTrace();
  } finally {
   closeStream(in, out);
  }
  return "uploadSucc";
 }
 public void closeStream(FileInputStream in, FileOutputStream out) {
  try {
   if (in != null) {
    in.close();
   }
   if (out != null) {
    out.close();
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
  ...
  setter() getter()
  ...  
}

获取进度的Action

public class FileProgressAction extends ActionSupport {
  private static final long serialVersionUID = 1L;
  private FileUploadStatus uploadStatus;

  public String uploadPercent() {
    HttpSession session = ServletActionContext.getRequest().getSession();
    this.uploadStatus = (FileUploadStatus) session.getAttribute("uploadStatus");
    if (uploadStatus == null) {
      System.out.println("action is null");
      uploadStatus = new FileUploadStatus();
      uploadStatus.setCurrentItems(0);
    }
    return "getPercent";
  }

  public FileUploadStatus getUploadStatus() {
    return uploadStatus;
  }

  public void setUploadStatus(FileUploadStatus uploadStatus) {
    this.uploadStatus = uploadStatus;
  }
}

5.struts.xml中

<struts>
  <constant name="struts.multipart.maxSize" value="2147483648"/><!-- 默认值为2M,设置为2G -->
  <constant name="struts.custom.i18n.resources" value="messageResource" />
  <constant name="struts.i18n.encoding" value="utf-8" />
  <constant name="struts.multipart.saveDir" value="e:/fileUpload"/><!-- 临时路径 -->
 
  <!-- 加载自定义的文件读取配置文件 -->
  <bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="Refactor" class="com.nova.core.RefactorMultiPartRequest" scope="default" />
  <constant name="struts.multipart.handler" value="Refactor" />
  <!-- 这里配置struts.multipart.handler -->
  <package name="ajaxUpload" extends="json-default"> <!-- json-default需要struts2-json-plugin-2.3.3.jar -->
   <action name="ajaxUploadFile_*" class="com.nova.action.FileStreamUploadAction" method="{1}">
    <result type="json" name="uploadSucc">
     <param name="root">newFileName</param>
     <param name="contentType"> 
      text/html
     </param> 
    </result> 
   </action>
   <action name="uploadPercent_*" class="com.nova.action.FileProgressAction" method="{1}">
    <result name="getPercent" type="json">
     <param name="root">uploadStatus</param>
    </result>
   </action>
  </package>
 </struts>


二.  进度条显示

View页面设置,利用ajaxfileupload.js来获取文件并进行异步上传,bootstrap中的进度条效果显示进度(利用setInterval间断的获取进度信息来形式一种进度的前进显示)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css" rel="external nofollow" >
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap-responsive.css" rel="external nofollow" >
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/ajaxfileupload.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/bootstrap/js/bootstrap.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/bootstrap/js/jquery.showLoading.min.js"></script>
<script type="text/javascript">
 var setinterval;
 $(document).ready(function(){
  $("#upload").click(function(){
   $("#upload").addClass("disabled");
   $("#upload").attr("disabled" ,true);
   $("#upload").attr("title" ,"文件上传中...");
   uploadFile();
   setinterval = setInterval(uploadProgress,200);
  });
 });
 //文件上传
 function uploadFile(){
  $.ajaxFileUpload({
   url:'ajaxUploadFile_uploadFile.action', 
   secureuri:false, //是否采用安全协议,默认为false
   fileElementId:'image',
   dataType: 'json',
   success: function (data){
    $("#showImage").attr("src","/FileUpLoadTest/file/"+data);
   }
  });
 }
 //上传进度
 function uploadProgress(){
  $.get("uploadPercent_uploadPercent.action","",function(data){
   $("#ProgressRate").html("上传速度:" + data.uploadRate + "%");
   $("#readBytes").html("以读取:" + data.readedBytes + " KB");
   $("#totalBytes").html("总大小:" + data.totalBytes + " KB");
   $("#progress").attr("style","width:" + data.uploadRate + "%;");
   $("#progress").html(data.uploadRate + "%");
   if(data.uploadRate == 100){
    clearInterval(setinterval);
    $("#progress").html("上传成功");
    $("#upload").removeClass("disabled");
    $("#upload").attr("disabled" ,false);
   }
  });
 }
</script>
</head>
<body>
 <div class="navbar navbar-inverse navbar-fixed-top">
  <div class="navbar-inner">
  <div class="container">
   <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
   <span class="icon-bar"></span>
   <span class="icon-bar"></span>
   <span class="icon-bar"></span>
   </button>
   <a class="brand" href="#" rel="external nofollow" >文件异步上传+进度条</a>
  </div>
  </div>
 </div>
 <br><br><br>
 <div class="container">
  <input type="file" name="image" id="image"/><br/> //file的name属性必须设置的与后台Action中file的名称是相同的,否则ajaxFileUpload获取不到文件信息
  <input type="button" id="upload" value="上传" class="btn btn-info" title=""/><br/>
  <img alt="" src="" id="showImage">
  <div id="ProgressRate"></div>
  <div id="readBytes"></div>
  <div id="totalBytes"></div>
  <div id="uploadTimes"></div>
  <div class="progress progress-striped span4">
    <div id="progress" class="bar">
    </div>
  </div>
 </div>
</body>
</html>


三、总结

  用这种方法获取上传进度有一个缺点:读取进度阶段是文件从指定目录开始在临时文件中存储的过程,而文件上传则是重临时路径下将文件转移到目标路径下,这样就造成了一个时间差,就是读取进度总会比上传文件快,上传的文件越大这个缺点越是明显。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# struts2  # 文件上传  # 进度条  # Struts2实现上传单个文件功能  # struts2实现多文件上传的示例代码  # java中Struts2 的文件上传和下载示例  # Struts2修改上传文件大小限制方法解析  # 上传  # 上传文件  # 序列化  # 很好  # 则是  # 找不到  # 不存在  # 自定义  # 总会  # 想看  # 造成了  # 越大  # 有两种  # 设置为  # 表单  # 长了  # 即在  # 配置文件 


相关文章: 高端云建站费用究竟需要多少预算?  历史网站制作软件,华为如何找回被删除的网站?  广平建站公司哪家专业可靠?如何选择?  中山网站推广排名,中山信息港登录入口?  广州网站设计制作一条龙,广州巨网网络科技有限公司是干什么的?  h5网站制作工具有哪些,h5页面制作工具有哪些?  整蛊网站制作软件,手机不停的收到各种网站的验证码短信,是手机病毒还是人为恶搞?有这种手机病毒吗?  python的本地网站制作,如何创建本地站点?  如何在IIS7中新建站点?详细步骤解析  北京制作网站的公司排名,北京三快科技有限公司是做什么?北京三快科技?  如何在阿里云ECS服务器部署织梦CMS网站?  如何在服务器上三步完成建站并提升流量?  如何用PHP工具快速搭建高效网站?  制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?  如何访问已购建站主机并解决登录问题?  百度网页制作网站有哪些,谁能告诉我百度网站是怎么联系?  微网站制作教程,不会写代码,不会编程,怎么样建自己的网站?  如何撰写建站申请书?关键要点有哪些?  深入理解Android中的xmlns:tools属性  常州企业网站制作公司,全国继续教育网怎么登录?  宝塔建站助手安装配置与建站模板使用全流程解析  电影网站制作价格表,那些提供免费电影的网站,他们是怎么盈利的?  焦点电影公司作品,电影焦点结局是什么?  专业的网站制作设计是什么,如何制作一个企业网站,建设网站的基本步骤有哪些?  成都网站制作报价公司,成都工业用气开户费用?  详解jQuery中基本的动画方法  建站之星如何助力网站排名飙升?揭秘高效技巧  已有域名建站全流程解析:网站搭建步骤与建站工具选择  制作旅游网站html,怎样注册旅游网站?  怎么用手机制作网站链接,dw怎么把手机适应页面变成网页?  ,怎么用自己头像做动态表情包?  网页制作模板网站推荐,网页设计海报之类的素材哪里好?  网站制作培训多少钱一个月,网站优化seo培训课程有哪些?  如何通过免费商城建站系统源码自定义网站主题与功能?  如何通过虚拟主机快速搭建个人网站?  建站之星如何快速更换网站模板?  定制建站流程解析:需求评估与SEO优化功能开发指南  如何在万网主机上快速搭建网站?  企业网站制作费用多少,企业网站空间一般需要多大,费用是多少?  用v-html解决Vue.js渲染中html标签不被解析的问题  网站企业制作流程,用什么语言做企业网站比较好?  如何正确下载安装西数主机建站助手?  网站制作话术技巧,网站推广做的好怎么话术?  微信h5制作网站有哪些,免费微信H5页面制作工具?  如何在Golang中指定模块版本_使用go.mod控制版本号  如何在Windows环境下新建FTP站点并设置权限?  建站之星备案流程有哪些注意事项?  做企业网站制作流程,企业网站制作基本流程有哪些?  公司网站建设制作费用,想建设一个属于自己的企业网站,该如何去做?  我的世界制作壁纸网站下载,手机怎么换我的世界壁纸? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。