How to fix an error `Java heap space` when downloading large files (GB) using Liferay

后端 未结 3 957
死守一世寂寞
死守一世寂寞 2021-01-26 16:03

I am using this code to download an existing file from the server on Liferay (6.2) into a local pc:

`

    File file = getFile(diskImage.getImageType(), d         


        
3条回答
  •  死守一世寂寞
    2021-01-26 16:44

    ServletResponseUtil.sendFile(httpReq, httpResp, file.getName(), input, "application/octet-stream"); what's this?

    Don't read a file once time.Use a buffer.

    response.reset();
    response.setContentType("application/x-download");
    response.addHeader("Content-Disposition","attachment;filename="+new String(filename.getBytes(),"utf-8"));
    response.addHeader("Content-Length",""+file.length());
    OutputStream toClient=new BufferedOutputStream(response.getOutputStream());
    response.setContentType("application/octet-stream");
    byte[] buffer=new byte[1024*1024*4];
    int i=-1;
    while((i=fis.read(buffer))!=-1){
      toClient.write(buffer,0,i);
    }
    fis.close();
    toClient.flush();
    toClient.close();
    

提交回复
热议问题