0

I am using struts2 framework for development. The users can download the image from our web page. The following code is worked but i have some question on the following code.

<result name="success" type="stream">
<param name="contentType">image/tiff</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">filename="test.tif"</param>
<param name="bufferSize">20480</param>
</result>

When user request the image, the system read the physical file and pass it into fileinputstream.

File f = new File("C:/test.tif");
fileInputStream = new FileInputStream(f);

however, if one or more users request the same images at the same times, is that any problems occur for the above code? If yes, how can i edit the code to avoid the concurrent problems?

  1. How can i read the physical file and then put it into memory file then pass the memory file to the client site?

Thanks

4

1 回答 1

0

Struts2 Actions 是 ThreadSafe 的

它们是ThreadLocal,每个动作都有自己的变量实例。

如果三个用户调用该代码,将创建三个 FileInputStream。

要将文件加载到内存中,您可以读取fileInputStream和写入ByteArrayOutputStream阅读更多),但是为什么要读取内存中的所有文件然后将其传递给客户端?它无用且昂贵,只需使用您的 inputStream 将其流式传输到客户端即可。如果设置 content-length,浏览器也会绘制进度条 :|

假设你有private InputStream fileInputStream;和相关的Getter,你的代码只缺少返回值:

File f = new File("C:/test.tif");
fileInputStream = new FileInputStream(f);
return SUCCESS;

由于java.io.File代表文件系统中的文件而不是打开的文件,因此您是安全的(下载完成后输入流将自动关闭)

于 2013-09-24T09:09:14.943 回答