1

当目录不存在以及文件已经在 Servlet 中使用相同的前缀创建时,我想处理异常。

如果我的 servlet 在收到请求后将 PDF 写入FileOutputStream并成功发送重定向到上一个收据页面,我不希望有一条消息解释写入请求范围的原因。

但是当目录不存在时(目录变量引入了类似的路径'D:\pdf')或者如果存在类似的命名文件,我想在请求范围内发送示例错误消息。

那么我的条件表达式是否正确?我是否正在使用FileOutputStream以及ServletOutputStream它的本意?我读过一个 servlet 可以发送到ServletOutputStream或写入到PrintWriter.

我将非常有义务提供任何指导或将我送到正确的方向。

这是servlet的重写方法,

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        System.out.println("headr " + request.getHeader("referer"));
        // response.setContentType("application/pdf");
        // response.setHeader("content-disposition", "attachment; filename=pdf.pdf");
        // response.setStatus(HttpServletResponse.SC_NO_CONTENT);  

        String prefix = request.getParameter("prefix");
        String no = request.getParameter("no");
        String html = request.getParameter("source");

        String directory = new SibsDelegate().getDirectoryPath();

        File file = new File(directory);
        if ( file.exists() && !new File(file, "prefix"+"-"+"no").exists())
        {
            System.out.println("exists");
           try 
           {
              HtmlCleaner cleaner = new HtmlCleaner();
              CleanerProperties props = cleaner.getProperties();

              TagNode node = cleaner.clean(html);

              // OutputStream os = response.getOutputStream();

              final XmlSerializer xmlSerializer = new PrettyXmlSerializer(props);
              final String html1 = xmlSerializer.getAsString(node);

              ITextRenderer renderer = new ITextRenderer();
              renderer.setDocumentFromString(html1);
              renderer.layout();

              String filename = prefix + "-" + no +".pdf";
              String fileNameWithPath = "D:\\pdf\\" + filename;
              FileOutputStream fos = new FileOutputStream( fileNameWithPath, false);
              renderer.createPDF( fos );
              fos.close();

              System.out.println( "PDF: '" + fileNameWithPath + "' created." );

              RequestDispatcher rd = null;
              // set message in request scope -- TODO
              request.getRequestDispatcher("Common_Receipt.jsp").forward(request, response);


              //  response.sendRedirect("Common_Receipt.jsp");


            } 
            catch (Exception ex) 
            {
               ex.printStackTrace();
            }
            finally
            {
                // fos.close();
            }

        }  

    }
4

1 回答 1

1

您的文件检查看起来不错,但我不会将其分成 2 种不同的错误类型以不同方式处理,而是将目录检查放在 try catch 之外,将文件检查放在里面,以便您可以使用以下方法处理错误IOExceptions

if (!file.exists() || !file.isDirectory()) {
    // handle directory doesnt exist
    // note that "error" is just a string variable you can make it whatever you want
    request.setAttribute("error", "Directory already exists!!!!!");
    // you can output this attribue in your jsp to tell the user there was an error
    request.getRequestDispatcher("Common_Receipt.jsp").forward(request, response);
}
FileOutputStream fos = null;
try {
    File outputFile = new File(file, "prefix"+"-"+"no");
    if(outputFile.exists()) {
        // handle file output already exists
        throw new FileAlreadyExistsException(prefix+"-"+no+".pdf");
    }
    fos = new FileOutputStream( outputFile, false);

    // do your writing to the file here
} catch(FileAlreadyExistsException e) {
    request.setAttribute("error", "your file already exists dummy");
    request.getRequestDispatcher("Common_Receipt.jsp").forward(request, response);
} catch(IOException e) {
    // had a problem writing to the file, you decide how to handle it, or group it with the FileAlreadyExistsException. Note that FileAlreadyExistsException extends IOException so to group then just put
    // your code from the FileAlreadyExistsException block into here
    request.setAttribute("error", "woah something bad happened");
    request.getRequestDispatcher("Common_Receipt.jsp").forward(request, response);
} finnaly {
    if(fos != null) fos.close();
}

让我知道这是否回答了您的问题,我在阅读您的问题时有点困惑。

于 2013-09-10T19:41:27.987 回答