Java Struts2:如何监控服务器端操作流的进度?

Java Struts2:如何监控服务器端操作流的进度?,java,struts2,stream,Java,Struts2,Stream,我有一个Struts 2操作,Struts 2位于服务器端,允许用户从我的应用程序下载文件 这些文件存储在一个数据库中(我知道……但要保持焦点)。 当用户访问该操作时,我从数据库中获取文件并使用流发送给用户 @Action(value="getFile", results= { @Result(name="ok", type="stream", params = { "contentType", "application/octet-strea

我有一个Struts 2操作,Struts 2位于服务器端,允许用户从我的应用程序下载文件

这些文件存储在一个数据库中(我知道……但要保持焦点)。 当用户访问该操作时,我从数据库中获取文件并使用流发送给用户

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

好的,伙计们。我在这里找到了窍门:

刚刚实现了我自己的侦听器:

public class ProgressListener implements OnProgressListener {

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

    }

}
并将我的Struts 2操作更改为:

@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 .

}
我转到时的结果打印在Tomcat控制台上(目前):


听起来很完美

已经有这样的问题了。@RomanC似乎不是重复的,因为他想监视从服务器下载的内容side@AlirezaFattahi当浏览器下载时,他似乎无法监视它,因此您需要创建一个组件来显示下载文件的进度。文件正在从服务器下载,但组件应该在客户端,否则称为日志。@RomanC您在我的副本中指出的问题是使用javascript,因此它的客户端。这不是我的情况。这个问题在哪里?Javascript是客户端,所以它很重要。我想在服务器端监视下载。Alireza Fattahi明白这一点,我会尽快接受答案。请看他的答案,您将看到服务器端和客户端下载监控之间的区别。如果我不使用客户端浏览器(如Java程序)下载,我如何应用您指出的答案,以及如何在服务器端监控各种客户端下载(如您的机器在将torrent文件播种到多个客户端时所做的)?
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%