我有一个 Struts 2 Action,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 流结果类不记录或生成这些数据。
您可以扩展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");
}
关于java - Struts2:如何监控服务器端 Action 流的进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31084303/