成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

Struts2實現(xiàn)文件上傳時顯示進(jìn)度條功能

       最近在做一個資源共享的項目中,采用了Struts2.1.8+Spring2.5.6+hibernate3.32的框架整合方式進(jìn)行開發(fā)。在文件上傳這塊,因為需要實現(xiàn)文件上傳時顯示進(jìn)度條的功能,所以嘗試了一下。怕以后忘記,先貼出來分享下。

創(chuàng)新互聯(lián)公司從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站建設(shè)、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元衡南做網(wǎng)站,已為上家服務(wù),為衡南各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220

       要在上傳文件時能顯示進(jìn)度條,首先需要實時的獲知web服務(wù)端接收了多少字節(jié),以及文件總大小,這里我們在頁面上使用AJAX技術(shù)每一秒向服務(wù)器發(fā)送一次請求來獲得需要的實時上傳信息。但是當(dāng)我們使用struts2后怎么在服務(wù)端獲得實時的上傳大小呢?這里需要用到commons-fileupload中的progressListener接口,實現(xiàn)這個接口,然后再實現(xiàn)一個自己的解析器,并在解析器中添加自己實現(xiàn)的那個progressListener;然后再替換struts2自帶的解析器(struts2自帶的解析器類沒有添加progressListener),然后就可以了。下面看看主要的代碼(技術(shù)有限,如有不對之處,望不吝點解): 

監(jiān)聽器:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.ProgressListener;

public class ResourceProgressListener implements ProgressListener {
 private HttpSession session;
 public ResourceProgressListener(HttpServletRequest request) {
  session = request.getSession();
  ResourceFileUploadStatus newUploadStatus = new ResourceFileUploadStatus();
  session.setAttribute("currentUploadStatus", newUploadStatus);

 }
 public void update(long readedBytes, long totalBytes, int currentItem) {
  ResourceFileUploadStatus status = (ResourceFileUploadStatus) session.getAttribute("currentUploadStatus");
  status.setReadedBytes(readedBytes);
  status.setTotalBytes(totalBytes);
  status.setCurrentItem(currentItem);

 }

}

上傳狀態(tài)類:

public class ResourceFileUploadStatus {
 private long readedBytes = 0L;
 private long totalBytes = 0L;
 private int currentItem = 0;

 public long getReadedBytes() {

  return readedBytes;
 }
 public void setReadedBytes(long bytes) {
  readedBytes = bytes;
 }

 public long getTotalBytes() {
  return totalBytes;
 }
 public void setTotalBytes(long bytes) {
  totalBytes = bytes;
 }
 public int getCurrentItem() {
  return currentItem;

 }

 public void setCurrentItem(int item) {
  currentItem = item;

 }

}

實現(xiàn)自己的解析器類:方法比較簡單,找到struts2實現(xiàn)的解析器類,把代碼拷貝過來然后添加上監(jiān)聽器即可。這個類代碼較多就不整個文件拷了,主要是在parse方法里添加。Parse方法代碼如下:紅色標(biāo)注部分即是需要自己添加的progressListener.

public void parse(HttpServletRequest servletRequest, String saveDir)
  throws IOException {

 System.out.println("執(zhí)行自定義MultiPartRequest");
  DiskFileItemFactory fac = new DiskFileItemFactory();
  // Make sure that the data is written to file
  fac.setSizeThreshold(0);
  if (saveDir != null) {
   fac.setRepository(new File(saveDir));
  }

  // Parse the request

  try {

   ServletFileUpload upload = new ServletFileUpload(fac);
   upload.setSizeMax(maxSize);

   ResourceProgressListener progressListener = new ResourceProgressListener(servletRequest);//新建一個監(jiān)聽器

    upload.setProgressListener(progressListener);//添加自己的監(jiān)聽器

   List items = upload.parseRequest(createRequestContext(servletRequest));

    for (Object item1 : items) {

    FileItem item = (FileItem) item1;
    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 = servletRequest.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");

 

     // Skip file uploads that don't have a file name - meaning that no file was selected.
     if (item.getName() == null || item.getName().trim().length() < 1) {
      LOG.debug("No file has been uploaded for the field: " + 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 (FileUploadException e) {
   LOG.warn("Unable to parse request", e);
   errors.add(e.getMessage());

  }

} 

上面的類建立完成后,還需要做一項工作:在struts.xml中添加如下內(nèi)容:

<bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="requestParser"
 class="com.zeige.ResourceMultiPartRequest" scope="default" optional="true" />
<constant name="struts.multipart.handler" value="requestParser" />

 下面就可以正常使用了,建立兩個action,一個用來接收上傳文件,以及對接收的文件作相應(yīng)處理,處理完成后,在return SUCCESS之前去除session中currentUploadStatus屬性,一個用來為頁面讀取實時上傳進(jìn)度服務(wù),這個類中只要將session中的currentUploadStatus對象拿出來按照相應(yīng)格式返回給客戶端即可。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁名稱:Struts2實現(xiàn)文件上傳時顯示進(jìn)度條功能
網(wǎng)頁網(wǎng)址:http://jinyejixie.com/article6/gpieig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、企業(yè)網(wǎng)站制作網(wǎng)站導(dǎo)航、品牌網(wǎng)站建設(shè)、定制網(wǎng)站、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化
广宁县| 阿勒泰市| 鄯善县| 奉化市| 秦皇岛市| 鲁山县| 安仁县| 潢川县| 邵武市| 苏尼特右旗| 全椒县| 叶城县| 上高县| 游戏| 宣恩县| 屏东县| 仁化县| 武乡县| 云南省| 宁河县| 泗水县| 白水县| 应用必备| 嘉禾县| 佛山市| 酉阳| 霍邱县| 长沙县| 丰宁| 巴马| 阳曲县| 澜沧| 延边| 高尔夫| 盐津县| 仲巴县| 安陆市| 邵阳县| 镇江市| 彩票| 漳平市|