FileDocCategorySizeDatePackage
FileViewerServlet.javaAPI DocExample1670Fri Jul 30 07:55:22 BST 2004com.oreilly.strutsckbk.ch07

FileViewerServlet

public class FileViewerServlet extends HttpServlet

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

        doPost(request, response);
    
protected voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

        String fileName = (String) request.getAttribute("fileName");
        fileName = "temp.txt";
        FileReader in = null;
        PrintWriter out = null;
        response.setContentType("text/plain");
        File dir = (File) getServletContext().getAttribute("javax.servlet.context.tempdir");
        File f = new File(dir, "test.tmp");
        try {
            // Get an input stream on the form file
            in = new FileReader(f);

            // Get an output stream for the response 
            out = response.getWriter();
        
            // Write from the input stream to the output stream
            char[] buffer = new char[512];
            int chars = 0;
            while ((chars = in.read(buffer)) != -1) {
                out.write(buffer, 0, chars);
            }
        }
        finally {
            if (out != null) out.close();
            if (in != null) in.close();     
        }