Send a file from server to client in GWT

五迷三道 提交于 2019-12-01 09:25:53

问题


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 of the document through a Servlet.

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?


回答1:


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; 



回答2:


You can create a standard servlet (which extends HttpServlet and not RemoteServiceServlet) on server side and opportunity to submit the id as servlet parameter on client side.

Now you need after getting request create the excel file and send it to the client. Browser shows automatically popup with download dialog box. But you should make sure that you set the right content-type response headers. This header will instruct the browser which type of file is it.

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) {
       } 
    } 
} 


来源:https://stackoverflow.com/questions/12110090/send-a-file-from-server-to-client-in-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!