Struts2文件下载:如何在文件创建期间优化内存使用

时间:2013-08-27 23:25:51

标签: struts2 download

Struts2的, 我使用下一个代码在服务器上创建文件,然后在用户计算机的硬盘上写入:

struts.xml中

<action name="download" class="com.DownloadAction">
  <result name="success" type="stream">
      <param name="contentType">application/octet-stream</param>
      <param name="inputName">fileInputStream</param>
      <param name="contentDisposition">attachment;filename="report.xxx"</param>
      <param name="bufferSize">83886080(or more bigger)</param>
  </result>
</action>    

动作

public String execute() throws Exception {

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    .... //code for writing to the buffer   

    fileInputStream = new ByteArrayInputStream(buffer.toByteArray());

    return SUCCESS;
}

当“缓冲区”非常大时,会出现java堆大小的问题

我想要下一个:

  1. 在“buffer”中创建新的数据段
  2. 将此段写入fileInputStream
  3. 为缓冲区中的此段分配的可用内存
  4. 重复步骤1),2)和3)
  5. 我该如何实现这样的算法?

0 个答案:

没有答案