2

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?

fgb
  • 18,439
  • 2
  • 38
  • 52
GameBuilder
  • 1,169
  • 4
  • 31
  • 62

2 Answers2

3

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; 
Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
1

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) {
       } 
    } 
} 
kapandron
  • 3,546
  • 2
  • 25
  • 38