【问题标题】:Send a file from server to client in GWT在 GWT 中从服务器向客户端发送文件
【发布时间】:2012-08-24 13:12:13
【问题描述】:

我正在使用 GWT。

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

文档在外部存储库中。

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

服务器端:使用这个ID文件被检索:

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();

在这里我可以阅读文档。

我想将此发送给客户。 如何将其制作成文件并将其发送回客户端?

【问题讨论】:

    标签: gwt servlets gwt-rpc gwt2 formpanel


    【解决方案1】:

    在您的 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; 
    

    【讨论】:

    • :从服务器下载到客户端的所有文件都是空的,但文件不是空的。
    • 我已经编辑了答案。立即尝试并检查您是否在 contentStream.getStream() 这是您的代码中获取数据。
    • :输入的内容。 new BufferedInputStream(input);
    • 抱歉错字:应该是“是”
    • :byte[] buf = new BufferedInputStream(inputStream); 这行给我错误 Type mismatch: cannot convert from BufferedInputStream to byte[] Change type to buf BufferInputStream
    【解决方案2】:

    您可以在服务器端创建一个标准 servlet(扩展 HttpServlet 而不是 RemoteServiceServlet),并有机会在客户端将 id 作为 servlet 参数提交。

    现在您需要在收到请求后创建 excel 文件并将其发送给客户端。浏览器会自动弹出下载对话框。 但是您应该确保设置正确的内容类型响应标头。此标头将指示浏览器它是哪种类型的文件。

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
                                                  throws ServletException, IOException { 
    
    String fileId = reguest.getParameter("fileId"); // value of file id from request
    File file = CreatorExel.getFile(fileId); // your method to create file from helper class
    
    // setting response headers
    response.setHeader("Content-Type", getServletContext().getMimeType(file.getName())); 
    response.setHeader("Content-Length", file.length()); 
    response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\""); 
    
    BufferedInputStream input = null; 
    BufferedOutputStream output = null; 
    
    try { 
        InputStream inputStream = new FileInputStream(file);
        ServletOutputStream outputStream = response.getOutputStream();
    
        input = new BufferedInputStream(fileInput); 
        output = new BufferedOutputStream(outputStream); 
    
        int count;
        byte[] buffer = new byte[8192]; //  buffer size is 512*16
        while ((count = input.read(buffer)) > 0) {
             output.write(buffer, 0, count);
        }
    
    } finally { 
        if (output != null) {
           try { 
              output.close(); 
           } catch (IOException ex) {
           } 
        }
        if (input != null) {
           try { 
              input.close(); 
           } catch (IOException ex) {
           } 
        } 
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 2017-01-12
      • 1970-01-01
      • 2014-09-07
      • 2014-10-08
      相关资源
      最近更新 更多