This is puzzling me. This happens be in a servlet that I modified. In
the code if a user tries to upload a file that is larger than the max
file size that is set in the servlet it throws an IO Exception. The
problem is in the catch I can't seem to redirect the user to a new
page (or print to the screen for that matter). Regardless of whether
I have the redirect in the catch or not, the user is simply returned
with your typical browser "Page not found" error. This is puzzling me
because the System.out.println is being reached in the catch, yet I
can do a redirect in the catch nor print to the screen, etc. Any
ideas what is causing the page to display the "page not found" error
and not handle a redirect in the catch block at the end???
Thank you for any help.
Rick

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import com.oreilly.servlet.multipart.*;

public class ParserUploadServlet extends HttpServlet {
        private File dir;

        public void init(ServletConfig config) throws ServletException {
                super.init(config);

        }

        public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
                PrintWriter out = response.getWriter();
                response.setContentType("text/plain");
                out.println("Parser Upload Servlet");


                try {
                        //rick added or moved around
                        String name = null;
                        String uploadDir = null;
                        String SaveAsFilename = null;
                        File dir = null;
                        //:~ end rick added

                        FilePart filePart = null;

                        MultipartParser mp = new MultipartParser(request, 
1*1024*1024); // 1MB
                        Part part;
                        while ((part = mp.readNextPart()) != null) {
                                name = part.getName();
                                if (part.isParam()) {
                                        // it's a parameter part
                                        ParamPart paramPart = (ParamPart) part;
                                        String value = paramPart.getStringValue();
                                        out.println("param; name=" + name + ", value=" 
+ value);

                                        //rick added
                                        if ( name.equals("uploadDir") )
                                        uploadDir = value;
                                        if ( name.equals("SaveAsFilename") )
                                        SaveAsFilename = value;
                                        //:~end rick added

                                }
                                else if (part.isFile()) {
                                        // it's a file part
                                        filePart = (FilePart) part;
                                }
                        }

                        //rick added
                        String pathAndFile = uploadDir + SaveAsFilename;
                        System.out.println("ParserUpload. pathAndfile = "+pathAndFile);
                        dir = new File(pathAndFile);
                        //:~ end rick added

                        String fileName = filePart.getFileName();
                        if (fileName != null) {
                                // the part actually contained a file
                                long size = filePart.writeTo(dir);
                                out.println("file; name=" + name + "; filename=" + 
fileName +
                                ", content type=" + filePart.getContentType() + " 
size=" + size);
                        }
                        else {
                                // the field did not contain a file
                                out.println("file; name=" + name + "; EMPTY");
                        }

                        out.flush();

                }

                catch (IOException lEx) {
                        System.out.println("PaserUploadServlet. catchIO Exception : 
"+lEx);
                        
response.sendRedirect("http://www.insidectm.com/ctmig/mainsite/faq.html");
                        return;
                        //this.getServletContext().log(lEx, "error reading or saving 
file");
                }
        }
}

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to