FileDocCategorySizeDatePackage
SendWord.javaAPI DocExample2255Tue May 13 15:31:52 BST 2003com.jspservletcookbook

SendWord

public class SendWord extends HttpServlet

Fields Summary
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

    
      //get the file name from the 'file' parameter
      String fileName = (String) request.getParameter("file");
      if (fileName == null || fileName.equals(""))
           throw new ServletException(
            "Invalid or non-existent file parameter in SendWord servlet.");
      
      // add the .doc suffix if it doesn't already exist
      if (fileName.indexOf(".doc") == -1)
          fileName = fileName + ".doc";
          
      //where are Word files kept?
     String wordDir = getServletContext().getInitParameter("word-dir");
     if (wordDir == null || wordDir.equals(""))
           throw new ServletException(
             "Invalid or non-existent wordDir context-param.");
          
      ServletOutputStream stream = null;
      BufferedInputStream buf = null;
     try{
     
     stream = response.getOutputStream();
     File doc = new File(wordDir + "/" + fileName);
     
      //set response headers
      response.setContentType("application/msword");
      
      response.addHeader(
        "Content-Disposition","attachment; filename="+fileName );

      response.setContentLength( (int) doc.length() );
      
     FileInputStream input = new FileInputStream(doc);
     buf = new BufferedInputStream(input);
     int readBytes = 0;

     //read from the file; write to the ServletOutputStream
     while((readBytes = buf.read()) != -1)
        stream.write(readBytes);

     } catch (IOException ioe){
     
        throw new ServletException(ioe.getMessage());
         
     } finally {
     
//close the input/output streams
     if(stream != null)
         stream.close();
      if(buf != null)
          buf.close();
          }
    
    
public voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

        
        doGet(request,response);