Struts2: 如何监控服务器端动作流的进度?
Struts2: How can I monitor the progress of an SERVER SIDE Action streaming?
我有一个 Struts 2 操作,Struts 2 在服务器端,允许用户从我的应用程序下载文件。
文件存储在数据库中(我知道...但请注意)。
用户访问操作,我从数据库中获取文件并使用 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 流结果 class 不记录或生成这些数据。
您可以扩展 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(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%
我觉得很完美!
我有一个 Struts 2 操作,Struts 2 在服务器端,允许用户从我的应用程序下载文件。
文件存储在数据库中(我知道...但请注意)。 用户访问操作,我从数据库中获取文件并使用 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 流结果 class 不记录或生成这些数据。
您可以扩展 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(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%
我觉得很完美!