我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

使用struts2实现一个文件上传功能并显示进度条

本篇文章为大家展示了使用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 values;
     if (params.get(item.getFieldName()) != null) {
      values = params.get(item.getFieldName());
     } else {
      values = new ArrayList();
     }
     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 values;
     if (files.get(item.getFieldName()) != null) {
      values = files.get(item.getFieldName());
     } else {
      values = new ArrayList();
     }
     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中


  
  
  
  
 
  
  
  
  
   
   
    
     newFileName
      
      text/html
      
     
   
   
    
     uploadStatus
    
   
  
 

二.  进度条显示

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




Insert title here









 
 



//file的name属性必须设置的与后台Action中file的名称是相同的,否则ajaxFileUpload获取不到文件信息

三、总结

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

上述内容就是使用struts2实现一个文件上传功能并显示进度条,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


新闻名称:使用struts2实现一个文件上传功能并显示进度条
地址分享:http://shouzuofang.com/article/isjddc.html

其他资讯