Struts2: How can I track the streaming of SERVER SIDE actions?

I have a Struts 2 action and Struts 2 on a SERVER SIDE so the user can upload files from my application.

Files are stored inside the database (I know ... but keep focus). The user gets access to the action, I get the file from the database and send it to the user using 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 ) {
                //
            }
...

Everything works the way I want, but I want to control (from my Tomcat container - SERVER SIDE, not the user browser) from the server side, the user loading process ...

Is it possible?

Thank.

+4
source share
2 answers

Ok guys. I found the trick here:

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(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%

!

+2

, . 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");
      }
+2

All Articles