Send a file from server to client in GWT

后端 未结 2 2005
萌比男神i
萌比男神i 2021-01-15 12:51

I am using GWT.

I have to download a file file from server to client.

Document is in the external repository.

Client sends the id o

2条回答
  •  萌比男神i
    2021-01-15 13:26

    In Your Servlet:

    Document document =(Document)session.getObject(docId);
    ContentStream contentStream = document.getContentStream();
    String name = contentStream.getFileName();
    response.setHeader("Content-Type", "application/octet-stream;");
    response.setHeader("Content-Disposition", "attachment;filename=\"" + name + "\"");
    OutputStream os = response.getOutputStream();
    InputStream is = 
      (ByteArrayInputStream) contentStream.getStream();
    BufferedInputStream buf = new BufferedInputStream(is);
    int readBytes=0;
    while((readBytes=buf.read())!=-1) {
          os.write(readBytes);
    }   
    os.flush();
    os.close();// *important*
    return; 
    

提交回复
热议问题