簡體   English   中英

Struts2:如何監控SERVER SIDE Action流的進度?

[英]Struts2: How can I monitor the progress of an SERVER SIDE Action streaming?

我有一個Struts 2 Action,Struts 2在SERVER SIDE,允許用戶從我的應用程序下載文件。

這些文件存儲在數據庫中(我知道......但保持焦點)。 用戶訪問操作,我從數據庫獲取文件並使用Stream發送給用戶。

@Action(value="getFile", results= {  
        @Result(name="ok", type="stream", params = {
                "contentType", "application/octet-stream",
                "inputName", "fileInputStream",
                "contentDisposition", "filename=\"${fileName}\"",
                "bufferSize", "1024"
        }) }
)  

...

public class GetFileAction {
    private String fileName;
    private Integer idFile;
    private ByteArrayInputStream fileInputStream;

    public String execute () {
            ...
            try {
                FileService fs = new FileService();
                if ( (idFile != null) && ( idFile > -1 ) ) {
                    file = fs.getFile( idFile );
                }

                if ( file != null ) {
                    byte[] theFile = file.getFile();

                    fileInputStream = new ByteArrayInputStream( theFile );
                    fileName = file.getFileName();
                } else {
                    //
                }

            } catch ( Exception e ) {
                //
            }
...

一切都按照我的意願工作,但我想從服務器端監控(從我的Tomcat容器 - 服務器端,而不是用戶的瀏覽器)用戶下載的進度...

可能嗎?

謝謝。

如果要監視服務器上的用戶下載,則需要定義自己的流結果類型。 由於struts流結果類不會記錄或生成這些數據。

您可以擴展StreamResult 請看看doExecute方法。 你可以看到

 while (-1 != (iSize = inputStream.read(oBuff))) {
                oOutput.write(oBuff, 0, iSize);
  }

在這里,您可以找到從文件中讀取的數據量。 你可以在這里放一些日志。 while循環重復每個緩沖區大小(默認為2048)。 此循環中的簡單計數器將顯示您已讀取的數據量

 int totalBlock = 0 ;
 while (-1 != (iSize = inputStream.read(oBuff))) {
           oOutput.write(oBuff, 0, iSize);
           totalBlock ++;
          log.debug( totalBlock * 2048  + "  is read so far");
      }

好,朋友們。 我在這里找到了訣竅:

http://www.java2s.com/Code/Android/File/InputStreamthatnotifieslistenersofitsprogress.htm

剛剛實現了我自己的監聽器:

public class ProgressListener implements OnProgressListener {

    @Override
    public void onProgress(int percentage, Object tag) {
        System.out.println( (String)tag + " " + percentage + "%" );

    }

}

並將我的Struts 2 Action更改為:

@Action(value="getFile", results= {  
        @Result(name="ok", type="stream", params = {
                "contentType", "application/octet-stream",
                "inputName", "fileInputStream",
                "contentDisposition", "filename=\"${fileName}\"",
                "bufferSize", "1024"
        }) }
)   

@ParentPackage("default")
public class GetFileAction extends BasicActionClass {
    private String fileName;
    private Integer idFile;
    private ProgressAwareInputStream fileInputStream;

    public String execute () {
        cmabreu.sagitarii.persistence.entity.File file = null;

        try {
            FileService fs = new FileService();
            if ( (idFile != null) && ( idFile > -1 ) ) {
                file = fs.getFile( idFile );
            }

            if ( file != null ) {
                fileName = file.getFileName();
                byte[] theFile = file.getFile();

                ProgressAwareInputStream pais = new ProgressAwareInputStream( new ByteArrayInputStream( theFile ), 
                        theFile.length, fileName );

                pais.setOnProgressListener( new ProgressListener() );

                fileInputStream = pais; 

            } else {
                //
            }

        } catch ( Exception e ) {
            //
        }

        return "ok";
    }

    ... getters and setters .

}

當我轉到http:// myapplication:8080 / getFile?idFile = 1234的結果打印在Tomcat控制台上(現在):

6FB377291.g6.txt.dot.gif 21%
6FB377291.g6.txt.dot.gif 42%
6FB377291.g6.txt.dot.gif 63%
6FB377291.g6.txt.dot.gif 84%
6FB377291.g6.txt.dot.gif 100%

對我來說聽起來很完美!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM