在 GWT 中从服务器向客户端发送文件 [英] Send a file from server to client in GWT

查看:29
本文介绍了在 GWT 中从服务器向客户端发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 GWT.

我必须从服务器下载一个文件文件到客户端.

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

文档位于外部存储库中.

Document is in the external repository.

客户端通过 Servlet 发送文档的 id.

Client sends the id of the document through a Servlet.

服务器端:使用此 ID 文档检索:

On server side: Using this ID document is retrieved:

Document document = (Document)session.getObject(docId);
ContentStream contentStream = document.getContentStream();

ByteArrayInputStream inputStream = (ByteArrayInputStream) contentStream.getStream();

int c;
while ((c = inputStream.read()) != -1) {
    System.out.print((char) c); 
}
String mime = contentStream.getMimeType();
String name = contentStream.getFileName();
InputStream strm = contentStream.getStream();

在这里我可以阅读文档.

Here I can read the document.

我想把这个发给客户.我如何将其设为文件并将其发送回客户端?

I want to send this to the client. How do I make this a file and send it back to the client?

推荐答案

在你的 Servlet 中:

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; 

这篇关于在 GWT 中从服务器向客户端发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆