在GWT中从服务器发送文件到客户端 [英] Send a file from server to client in GWT
本文介绍了在GWT中从服务器发送文件到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用GWT。
我必须从服务器下载文件到客户。
文档位于外部存储库中。
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中: p>
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屋!
查看全文