struts2:上傳多個文件時實現帶進度條、進度詳細信息的示範

上一篇文章講了上傳單個文件與上傳多個文件(屬性驅動)的例子。本例是上傳多個文件(屬性驅動),而且顯示進度條、進度詳細信息的示範。javascript

  • 在文件上傳選擇界面,容許用戶增長、刪除選擇的文件,且只能上傳指定類型的文件;
  • 在文件上傳選擇界面,不容許用戶直接輸入文件名,必須經過按鈕選擇文件;
  • 上傳過程當中,利用jQuery的progressbar的widget插件顯示當前上傳進度的百分比;
  • 上傳過程當中,利用一個DIV顯示上傳進度的詳細信息。

目錄

1. 設計上傳的JSP頁面(uploadTest3.jsp)
  1.1 關於jQuery的進度條,有三段代碼
  1.2 輪詢後臺查詢進度
2. 顯示處理結果的JSP頁面(showResult2.jsp)
3. 建立Action類及屬性驅動類
4. 建立實現ProgressListener接口的類、建立實現查詢進度的Action類、建立傳遞進度信息的實體類
5. 將上傳進度類與Struts2關聯
  5.1 拷貝JakartaMultiPartRequest類並作適當修改
  5.2 替換Struts2默認的上傳關聯類(修改struts.xml)
6. 修改struts.xml,加入Action配置
7. 設置上傳文件相關參數(struts.properties)
8. 測試css

1. 設計上傳的JSP頁面(uploadTest3.jsp)

對比於上一篇文章中的uploadTest2.jsp文件,加入了以下功能:html

  • 增長、刪除上傳文件,只能上傳指定類型的文件;只能經過按鈕選擇文件,不可能直接輸入文件名。方法:file_change()/remove_file()/insertFile()/f()等。
  • 增長調用jQuery的AJAX方法輪詢後臺上傳進度信息並顯示在前臺。方法:refreshProcessBar()/refreshProcessBarCallBack()/及function()初始化進度條等。
  • 增長了引用jQuery進度條(progressbar)的相關CSS與JS文件:jquery.js/jquery.ui.core.js/jquery.ui.widget.js/jquery.ui.progressbar.js;至於CSS文件,爲簡單起見,我直接將示範目錄themes/base/拷貝到項目中。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ page isELIgnored="false" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>多文件上傳,顯示進度條實例</title>
<style type="text/css">
<!--
body, td, th {
    font-size: 9pt;
}
-->
</style>
<!--參考:http://api.jqueryui.com/progressbar/-->
<link rel="stylesheet" href="../../css/themes/base/jquery.ui.all.css">
<script src="../../js/jquery.js"></script>
<script src="../../js/jquery.ui.core.js"></script>
<script src="../../js/jquery.ui.widget.js"></script>
<script src="../../js/jquery.ui.progressbar.js"></script>
<script type="text/javascript">
    // 下面這三個函數是生成與刷新進度條、進度詳細信息的
    // 初始化進度條
    $(function() {
        $("#progressbar").progressbar({
            value: 0
        });
    });
    
    // 調用查詢進度信息接口
    function refreshProcessBar(){
        $.get("getState.action?timestamp=" + new Date().getTime(), function(data){
            refreshProcessBarCallBack(data);
        }, 'xml');
    }
    
    // 查詢進度信息接口回調函數
    function refreshProcessBarCallBack(returnXMLParam){
        var returnXML = returnXMLParam;
        var percent = $(returnXML).find('percent').text()
        var showText = "進度是:" + percent + "%";
        showText = showText + "\n當前上傳文件大小爲:" + $(returnXML).find ('uploadByte').text();
        showText = showText + "\n上傳文件總大小爲:" + $(returnXML).find ('fileSizeByte').text();
        showText = showText + "\n當前上傳文件爲第:" + $(returnXML).find ('fileIndex').text() + "";
        showText = showText + "\n開始上傳時間:" + $(returnXML).find ('startTime').text();
        
        // 刷新進度詳細信息
        $('#progressDetail').empty();
        $('#progressDetail').text(showText);
        
        // 刷新進度條
        $("#progressbar").progressbar("option", "value", parseInt(percent));
        
        setTimeout("refreshProcessBar()", 1000);
    }
    
    // 下面這三個函數是控制添加、刪除、修改附件的(容許增長、刪除附件,只容許指定後綴的文件被選擇等)
    var a = 0;
    function file_change(){
        //當文本域中的值改變時觸發此方法
        var postfix = this.value.substring(this.value.lastIndexOf(".") + 1).toUpperCase();
        //判斷擴展是否合法
        if (postfix == "JPG" || postfix == "GIF" || postfix == "PNG" || postfix == "BMP" ||
            postfix == "RAR" ||
            postfix == "ZIP" ||
            postfix == "TXT" ||
            postfix == "GHO" ||
            postfix == "PDF") {
        }
        else {
            //若是不合法就刪除相應的File表單及br標籤
            alert("您上傳的文件類型不被支持,本系統只支持JPG,GIF,PNG,BMP,RAR,ZIP,TXT文件!");
            var testtest = $(this).attr('id');
            testtest = '#' + testtest;
            var sub_file = $(testtest);
            
            var next_a_ele = sub_file.next();//取得a標記
            var br1_ele = $(next_a_ele).next();//取得回車
            var br2_ele = $(br1_ele).next();//取得回車
            
            $(br2_ele).remove();//刪除回車
            $(br1_ele).remove();//刪除回車
            $(next_a_ele).remove();//刪除a標籤
            $(sub_file).remove();
            //刪除文本域,由於上傳的文件類型出錯,要刪除動態建立的File表單
            return;
        }
    }
    
    function remove_file(){//刪除File表單域的方法
        //刪除表單
        var testtest = $(this).val();
        testtest = '#' + testtest;
        var sub_file = $(testtest);
        
        var next_a_ele = sub_file.next();//取得a標記
        var br1_ele = $(next_a_ele).next();//取得回車
        var br2_ele = $(br1_ele).next();//取得回車
        
        $(br2_ele).remove();//刪除回車
        $(br1_ele).remove();//刪除回車
        $(next_a_ele).remove();//刪除a標籤
        $(sub_file).remove();//刪除File標記
    }
    
    function f(){
        //方法名爲f的主要做用是不容許在File表單域中手動輸入文件名,必須單擊「瀏覽」按鈕
        return false;
    }
    
    function insertFile(){
        //新建File表單
        var file_array = document.getElementsByTagName("input");
        
        var is_null = false;
        //循環遍歷判斷是否有某一個File表單域的值爲空
        for (var i = 0; i < file_array.length; i++) {
            if (file_array[i].type == "file" && file_array[i].name.substring(0, 15) == "fileUploadTools") {
                if (file_array[i].value == "") {
                    alert("某一附件爲空不能繼續添加");
                    is_null = true;
                    break;
                }
            }
        }
        
        if (is_null) {
            return;
        }
        
        a++;
        //新建file表單的基本信息
        var new_File_element = $('<input>');
        new_File_element.attr('type', 'file');
        new_File_element.attr('id', 'uploadFile' + a);
        new_File_element.attr('name', 'fileUploadTools.uploadFile');
        new_File_element.attr('size', 55);
        new_File_element.keydown(f);
        new_File_element.change(file_change);
        
        $('#fileForm').append(new_File_element);
        
        //新建刪除附件的a標籤的基本信息
        var new_a_element = $('<a>');
        new_a_element.html("刪除附件");
        new_a_element.attr('id', "a_" + new_File_element.name);
        new_a_element.attr('name', "a_" + new_File_element.name);
        new_a_element.val($(new_File_element).attr('id'));
        new_a_element.attr('href', "#");
        new_a_element.click(remove_file);
        $('#fileForm').append(new_a_element);
        
        var new_br_element = $("<br>");
        $('#fileForm').append(new_br_element);
        var new_br_element = $("<br>");
        $('#fileForm').append(new_br_element);
    }
    
    // 提交表單,提交時觸發刷新進度條函數
    function submitForm(){
        setTimeout("refreshProcessBar()", 1000);
        
        return true;
    }
</script>
</head>
<body>
    <br/>
    <s:form action="uploadT2" method="post" enctype="multipart/form-data" onsubmit="return submitForm()">
        <table width="818" border="1">
            <tr>
                <td width="176">
                    <div align="center">
                        用戶帳號
                    </div>
                </td>
                <td width="626">
                    <input type="text" name="fileUploadTools.username" />
                </td>
            </tr>
            <tr>
                <td>
                    <div align="center">
                        用戶附件
                        <br/>
                        <a href="javascript:insertFile()">添加附件</a>
                    </div>
                </td>
                <td id="fileForm">
                    <br/>
                </td>
            </tr>
        </table>
        <input type="submit" value="開始上傳..." />
    </s:form>
    <br/>
    <div id="progressbar" style="width:500"></div>
    <br/>
    <div id="progressDetail" class="demo-description">
    <p>進度詳細信息顯示於此......</p>
    </div>
</body>
</html>
View Code

由於代碼太長,因此縮起來了。java

1.1 關於jQuery的進度條,有三段代碼

引入CSS及JS文件jquery

須要注意jquery.ui.all.css這個CSS文件會引用其它CSS文件,而後沒止境的引用下去,因此,最好的辦法,拷貝整個themes/base目錄到項目中最好。ajax

themes/base目錄位於jquery-ui-1.10.3.zip包中。express

progressbar的API官網:http://api.jqueryui.com/progressbar/apache

<link rel="stylesheet" href="../../css/themes/base/jquery.ui.all.css">
<script src="../../js/jquery.js"></script>
<script src="../../js/jquery.ui.core.js"></script>
<script src="../../js/jquery.ui.widget.js"></script>
<script src="../../js/jquery.ui.progressbar.js"></script>

初始化進度條api

    // 初始化進度條
    $(function() {
        $("#progressbar").progressbar({
            value: 0
        });
    });

更新進度條數組

        // 刷新進度條
        $("#progressbar").progressbar("option", "value", parseInt(percent));

1.2 輪詢後臺查詢進度

表單提交時,觸發refreshProcessBar()方法,回調refreshProcessBarCallBack()方法,而後再觸發refreshProcessBar()方法,,,,,,, 這樣能夠實時查詢上傳進度,並更新前臺頁面。

2. 顯示處理結果的JSP頁面(showResult2.jsp)

這個頁面就是上一篇文章中的頁面,如出一轍的。爲縮小篇幅,代碼縮起來。

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page isELIgnored="false"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <body>
        您上傳的文件名列表:
        <br/>
        <!--對Action中對象(fileUploadTools)包含的數組(uploadFileFileName)的遍歷-->
        <s:iterator value="fileUploadTools.uploadFileFileName" status="st">
            <s:iterator value="fileUploadTools.uploadFileFileName[#st.index]"> 
                <s:property />
                <br/>
            </s:iterator>
        </s:iterator>
        <br />
    </body>
</html>
View Code

3. 建立Action類及屬性驅動類

UploadTest2Action.java

這個Action類與上一篇文章中的Action類是如出一轍的,爲縮小篇幅,代碼縮起來。

package com.clzhang.struts2.demo12;

import java.io.IOException;

import com.opensymphony.xwork2.ActionSupport;

public class UploadTest2Action extends ActionSupport {
    public static final long serialVersionUID = 1;

    // 聲明封裝了File上傳的FileUploadTools類的實例
    // FileUploadTools類也封裝了上傳的屬性及get和set方法
    private FileUploadTools fileUploadTools = new FileUploadTools();

    public FileUploadTools getFileUploadTools() {
        return fileUploadTools;
    }
    public void setFileUploadTools(FileUploadTools fileUploadTools) {
        this.fileUploadTools = fileUploadTools;
    }

    @Override
    public String execute() throws IOException {
        fileUploadTools.beginUpload();
        
        return SUCCESS;
    }
}
View Code

FileUploadTools.java

這個屬性代碼類與上一篇文章中的Action類也是如出一轍的,爲縮小篇幅,代碼縮起來。

package com.clzhang.struts2.demo12;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

public class FileUploadTools {
    private String username;
    private File uploadFile[];// 上傳的文件是數組類型
    private String uploadFileFileName[];// 文件名是數組類型
    private String uploadFileContentType[];

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    // 上傳的ContentType文件類型也是數組類型
    // 必需要加上對ContentType的聲明,不然會出現異常
    public String[] getUploadFileContentType() {
        return uploadFileContentType;
    }
    public void setUploadFileContentType(String[] uploadFileContentType) {
        this.uploadFileContentType = uploadFileContentType;
    }
    public File[] getUploadFile() {
        return uploadFile;
    }
    public void setUploadFile(File[] uploadFile) {
        this.uploadFile = uploadFile;
    }
    public String[] getUploadFileFileName() {
        return uploadFileFileName;
    }
    public void setUploadFileFileName(String[] uploadFileFileName) {
        this.uploadFileFileName = uploadFileFileName;
    }

    public String beginUpload() throws IOException {
        System.out.println("用戶名:" + username);

        String targetDirectory = ServletActionContext.getServletContext().getRealPath("/upload");
        for (int i = 0; i < uploadFile.length; i++) {
            File target = new File(targetDirectory, new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss")
                    .format(new Date()).toString() + System.nanoTime() + uploadFileFileName[i]);

            FileUtils.copyFile(uploadFile[i], target);
        }

        return "success";
    }
}
View Code

4. 建立實現ProgressListener接口的類、建立實現查詢進度的Action類、建立傳遞進度信息的實體類

這個是查詢進度信息的核心。

  • State類是一個保存進度信息的實體類,屬性包括:uploadByte上傳了多少字節,fileSizeByte文件總大小,fileIndex正在上傳第幾個文件等等;
  • FileUploadListener類是一個實現ProgressListener接口的類,會在上傳文件時,將接收到的相關信息存儲到State類中;
  • GetState是一個Action類,是將State當前狀態返回給前臺的接口,由前臺AJAX調用,因此execute()方法返回null便可。

State.java

package com.clzhang.struts2.demo12;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class State {
    private long uploadByte;  //已經上傳的字節數,單位:字節
    private long fileSizeByte; //全部文件的總長度,單位:字節
    private int fileIndex; //正在上傳第幾個文件
    private long startTime; //開始上傳的時間,用於計算上傳速度等
    private int percent; // 上傳百分比
    
    private static final SimpleDateFormat SIMPLEFORMAT = new SimpleDateFormat("HH:mm:ss");
    public State() {
        startTime = System.currentTimeMillis();
        percent = 0;
    }

    // 從State狀態類中取得狀態的字符串,用字符串的形式拼成XML文件內容
    public synchronized String getStateString() {
        StringBuilder sb = new StringBuilder("<info>");
        sb.append("<uploadByte>" + NumberFormat.getInstance().format(uploadByte) + "</uploadByte>");
        sb.append("<fileSizeByte>" + NumberFormat.getInstance().format(fileSizeByte) + "</fileSizeByte>");
        sb.append("<fileIndex>" + fileIndex + "</fileIndex>");
        sb.append("<percent>" + percent + "</percent>");
        sb.append("<startTime>" + SIMPLEFORMAT.format(startTime) + "</startTime>");
        sb.append("</info>");
        
        return sb.toString();
    }

    public synchronized void setState(long uploadByte, long fileSizeByte, int fileIndex) {
        this.uploadByte = uploadByte;
        this.fileSizeByte = fileSizeByte;
        this.fileIndex = fileIndex;

        if ((Long.valueOf(uploadByte) * 100 / Long.valueOf(fileSizeByte) <= 100)) {
            // 生成當前上傳進度的公式,加入判斷條件的含義在於不須要重複計算
            percent = (int)(Long.valueOf(uploadByte) * 100 / Long.valueOf(fileSizeByte));
        }
    }
}

FileUploadListener.java

package com.clzhang.struts2.demo12;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.ProgressListener;

public class FileUploadListener implements ProgressListener {
    // 聲明一個HttpSession,目的是把State對象放到這個HttpSession中
    private HttpSession session;

    // 此構造函數由MyJakartaMultiPartRequest.java類parseRequest()方法調用
    public FileUploadListener(HttpServletRequest request) {
        super();
        session = request.getSession();
    }

    public void update(long uploadByte, long fileSizeByte, int fileIndex) {
        if (fileSizeByte == -1) {
            // 若是上傳的大小爲-1則上傳已經完成
            System.out.println("上傳文件結束!");
        } else {
            if (session.getAttribute("uploadState") == null) {
                // 若是爲空就new一個State對象並設置裏面的文本內容
                State state = new State();
                state.setState(uploadByte, fileSizeByte, (fileIndex - 1));
                session.setAttribute("uploadState", state);
            } else {
                // 若是session中有uploadState對象就取出來,而後設置裏面文本內容
                State state = (State) session.getAttribute("uploadState");
                state.setState(uploadByte, fileSizeByte, (fileIndex - 1));
            }
        }
    }
}

GetState.java

package com.clzhang.struts2.demo12;

import java.io.IOException;
import org.apache.struts2.ServletActionContext;

public class GetState {
    public String execute() throws IOException {
        // 從session中取得名稱爲uploadState的State對象
        State tempState = (State) ServletActionContext.getRequest().getSession()
                .getAttribute("uploadState");
        // 設置編碼爲utf-8
        ServletActionContext.getResponse().setCharacterEncoding("utf-8");
        // 設置響應的格式爲XML
        ServletActionContext.getResponse().setContentType("text/xml");
        // 用out對象輸出xml代碼頭
        ServletActionContext.getResponse().getWriter()
                .print("<?xml version='1.0' encoding='" + "utf-8" + "' ?>");
        // 用out對象輸出xml代碼體
        ServletActionContext.getResponse().getWriter().print(tempState.getStateString());
        
        return null;
    }
}

5. 將上傳進度類與Struts2關聯

須要替換掉Struts2封裝的上傳外包類,以使fileUpload與咱們上面的監聽器綁定起來。

5.1 拷貝JakartaMultiPartRequest類並作適當修改

將org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest類的源代碼拷貝到項目中,更改以下代碼:

import org.apache.struts2.dispatcher.multipart.MultiPartRequest;

    private List<FileItem> parseRequest(HttpServletRequest servletRequest, String saveDir) throws FileUploadException {
        DiskFileItemFactory fac = createDiskFileItemFactory(saveDir);
        ServletFileUpload upload = new ServletFileUpload(fac);
        upload.setProgressListener(new FileUploadListener(servletRequest));// 設置上傳進度的監聽
        upload.setSizeMax(maxSize);
        return upload.parseRequest(createRequestContext(servletRequest));
    }

注意代碼中的upload.setProgressListener(new FileUploadListener(servletRequest));行,該語句將自定義監聽器與上傳組件結合了起來。

完整代碼以下(struts-2.3.8):

/*
 * $Id: JakartaMultiPartRequest.java 1384107 2012-09-12 20:14:23Z lukaszlenart $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.clzhang.struts2.demo12;

import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.LocalizedTextUtil;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.StrutsConstants;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.apache.struts2.dispatcher.multipart.MultiPartRequest;

/**
 * Multipart form data request adapter for Jakarta Commons Fileupload package.
 */
public class MyJakartaMultiPartRequest implements MultiPartRequest {

    static final Logger LOG = LoggerFactory.getLogger(MyJakartaMultiPartRequest.class);

    // maps parameter name -> List of FileItem objects
    protected Map<String, List<FileItem>> files = new HashMap<String, List<FileItem>>();

    // maps parameter name -> List of param values
    protected Map<String, List<String>> params = new HashMap<String, List<String>>();

    // any errors while processing this request
    protected List<String> errors = new ArrayList<String>();

    protected long maxSize;
    private Locale defaultLocale = Locale.ENGLISH;

    @Inject(StrutsConstants.STRUTS_MULTIPART_MAXSIZE)
    public void setMaxSize(String maxSize) {
        this.maxSize = Long.parseLong(maxSize);
    }

    @Inject
    public void setLocaleProvider(LocaleProvider provider) {
        defaultLocale = provider.getLocale();
    }

    /**
     * Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
     * multipart classes (see class description).
     *
     * @param saveDir the directory to save off the file
     * @param request the request containing the multipart
     * @throws java.io.IOException is thrown if encoding fails.
     */
    public void parse(HttpServletRequest request, String saveDir) throws IOException {
        try {
            setLocale(request);
            processUpload(request, saveDir);
        } catch (FileUploadBase.SizeLimitExceededException 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) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Unable to parse request", e);
            }
            String errorMessage = buildErrorMessage(e, new Object[]{});
            if (!errors.contains(errorMessage)) {
                errors.add(errorMessage);
            }
        }
    }

    protected void setLocale(HttpServletRequest request) {
        if (defaultLocale == null) {
            defaultLocale = request.getLocale();
        }
    }

    protected String buildErrorMessage(Throwable e, Object[] args) {
        String errorKey = "struts.messages.upload.error." + e.getClass().getSimpleName();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Preparing error message for key: [#0]", errorKey);
        }
        return LocalizedTextUtil.findText(this.getClass(), errorKey, defaultLocale, e.getMessage(), args);
    }

    private void processUpload(HttpServletRequest request, String saveDir) throws FileUploadException, UnsupportedEncodingException {
        for (FileItem item : parseRequest(request, saveDir)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found item " + item.getFieldName());
            }
            if (item.isFormField()) {
                processNormalFormField(item, request.getCharacterEncoding());
            } else {
                processFileField(item);
            }
        }
    }

    private void processFileField(FileItem item) {
        if (LOG.isDebugEnabled()) {
            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());
            return;
        }

        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);
    }

    private void processNormalFormField(FileItem item, String charset) throws UnsupportedEncodingException {
        if (LOG.isDebugEnabled()) {
            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>();
        }

        // note: see http://jira.opensymphony.com/browse/WW-633
        // basically, in some cases the charset may be null, so
        // we're just going to try to "other" method (no idea if this
        // will work)
        if (charset != null) {
            values.add(item.getString(charset));
        } else {
            values.add(item.getString());
        }
        params.put(item.getFieldName(), values);
        item.delete();
    }

    private List<FileItem> parseRequest(HttpServletRequest servletRequest, String saveDir) throws FileUploadException {
        DiskFileItemFactory fac = createDiskFileItemFactory(saveDir);
        ServletFileUpload upload = new ServletFileUpload(fac);
        upload.setProgressListener(new FileUploadListener(servletRequest));// 設置上傳進度的監聽
        upload.setSizeMax(maxSize);
        return upload.parseRequest(createRequestContext(servletRequest));
    }

    private DiskFileItemFactory createDiskFileItemFactory(String saveDir) {
        DiskFileItemFactory fac = new DiskFileItemFactory();
        // Make sure that the data is written to file
        fac.setSizeThreshold(0);
        if (saveDir != null) {
            fac.setRepository(new File(saveDir));
        }
        return fac;
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFileParameterNames()
     */
    public Enumeration<String> getFileParameterNames() {
        return Collections.enumeration(files.keySet());
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getContentType(java.lang.String)
     */
    public String[] getContentType(String fieldName) {
        List<FileItem> items = files.get(fieldName);

        if (items == null) {
            return null;
        }

        List<String> contentTypes = new ArrayList<String>(items.size());
        for (FileItem fileItem : items) {
            contentTypes.add(fileItem.getContentType());
        }

        return contentTypes.toArray(new String[contentTypes.size()]);
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFile(java.lang.String)
     */
    public File[] getFile(String fieldName) {
        List<FileItem> items = files.get(fieldName);

        if (items == null) {
            return null;
        }

        List<File> fileList = new ArrayList<File>(items.size());
        for (FileItem fileItem : items) {
            File storeLocation = ((DiskFileItem) fileItem).getStoreLocation();
            if (fileItem.isInMemory() && storeLocation != null && !storeLocation.exists()) {
                try {
                    storeLocation.createNewFile();
                } catch (IOException e) {
                    if (LOG.isErrorEnabled()) {
                        LOG.error("Cannot write uploaded empty file to disk: " + storeLocation.getAbsolutePath(), e);
                    }
                }
            }
            fileList.add(storeLocation);
        }

        return fileList.toArray(new File[fileList.size()]);
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFileNames(java.lang.String)
     */
    public String[] getFileNames(String fieldName) {
        List<FileItem> items = files.get(fieldName);

        if (items == null) {
            return null;
        }

        List<String> fileNames = new ArrayList<String>(items.size());
        for (FileItem fileItem : items) {
            fileNames.add(getCanonicalName(fileItem.getName()));
        }

        return fileNames.toArray(new String[fileNames.size()]);
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFilesystemName(java.lang.String)
     */
    public String[] getFilesystemName(String fieldName) {
        List<FileItem> items = files.get(fieldName);

        if (items == null) {
            return null;
        }

        List<String> fileNames = new ArrayList<String>(items.size());
        for (FileItem fileItem : items) {
            fileNames.add(((DiskFileItem) fileItem).getStoreLocation().getName());
        }

        return fileNames.toArray(new String[fileNames.size()]);
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getParameter(java.lang.String)
     */
    public String getParameter(String name) {
        List<String> v = params.get(name);
        if (v != null && v.size() > 0) {
            return v.get(0);
        }

        return null;
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getParameterNames()
     */
    public Enumeration<String> getParameterNames() {
        return Collections.enumeration(params.keySet());
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getParameterValues(java.lang.String)
     */
    public String[] getParameterValues(String name) {
        List<String> v = params.get(name);
        if (v != null && v.size() > 0) {
            return v.toArray(new String[v.size()]);
        }

        return null;
    }

    /* (non-Javadoc)
     * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getErrors()
     */
    public List<String> getErrors() {
        return errors;
    }

    /**
     * Returns the canonical name of the given file.
     *
     * @param filename the given file
     * @return the canonical name of the given file
     */
    private String getCanonicalName(String filename) {
        int forwardSlash = filename.lastIndexOf("/");
        int backwardSlash = filename.lastIndexOf("\\");
        if (forwardSlash != -1 && forwardSlash > backwardSlash) {
            filename = filename.substring(forwardSlash + 1, filename.length());
        } else if (backwardSlash != -1 && backwardSlash >= forwardSlash) {
            filename = filename.substring(backwardSlash + 1, filename.length());
        }

        return filename;
    }

    /**
     * Creates a RequestContext needed by Jakarta Commons Upload.
     *
     * @param req the request.
     * @return a new request context.
     */
    private RequestContext createRequestContext(final HttpServletRequest req) {
        return new RequestContext() {
            public String getCharacterEncoding() {
                return req.getCharacterEncoding();
            }

            public String getContentType() {
                return req.getContentType();
            }

            public int getContentLength() {
                return req.getContentLength();
            }

            public InputStream getInputStream() throws IOException {
                InputStream in = req.getInputStream();
                if (in == null) {
                    throw new IOException("Missing content in the request");
                }
                return req.getInputStream();
            }
        };
    }

    /* (non-Javadoc)
    * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#cleanUp()
    */
    public void cleanUp() {
        Set<String> names = files.keySet();
        for (String name : names) {
            List<FileItem> items = files.get(name);
            for (FileItem item : items) {
                if (LOG.isDebugEnabled()) {
                    String msg = LocalizedTextUtil.findText(this.getClass(), "struts.messages.removing.file",
                            Locale.ENGLISH, "no.message.found", new Object[]{name, item});
                    LOG.debug(msg);
                }
                if (!item.isInMemory()) {
                    item.delete();
                }
            }
        }
    }

}
View Code

5.2 替換Struts2默認的上傳關聯類(修改struts.xml)

    <!--demo12,struts2文件上傳與下載-->
    <constant name="struts.multipart.handler" value="MyJakartaMultiPartRequestRef" />

    <!--demo12,struts2文件上傳與下載-->
    <bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest"
        name="MyJakartaMultiPartRequestRef"
        class="com.clzhang.struts2.demo12.MyJakartaMultiPartRequest" scope="default"/>

至此,上傳文件時將觸發咱們上面寫的FileUploadListener類,捕捉到相關上傳進度信息。

6. 修改struts.xml,加入Action配置

        <action name="uploadT2" class="com.clzhang.struts2.demo12.UploadTest2Action">
            <result>/struts2/demo12/showResult2.jsp</result>
        </action>
        <action name="getState" class="com.clzhang.struts2.demo12.GetState"></action>

第一個Action爲提交上傳文件表單時用的;第二個Action爲輪詢上傳進度信息用的。

7. 設置上傳文件相關參數(struts.properties)

同前一篇文章。

struts.multipart.maxSize=2048000000
struts.multipart.saveDir=/upload

8. 測試

打開IE,輸入地址:http://127.0.0.1:8080/st/struts2/demo12/uploadTest3.jsp

結果以下:

選擇幾個較大的文件,提交:

最終結果:

相關文章
相關標籤/搜索