servlet无法在我的webcontent文件夹中找到文件

时间:2012-11-20 16:14:07

标签: java servlets

这是我的Servlet代码片段,它生成PDF然后打开它。它无法打开“AvtoSolaZaposleniXSL.xsl”文件。如果我在普通的Java类中运行相同的过程,一切都会顺利进行。

public class PDF extends HttpServlet {
private static final long serialVersionUID = 1L;

public PDF() { super(); }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //Generiraj PDF
    File xmlfile = new File(getServletContext().getRealPath("AvtoSolaZ.xml"));
    File xsltfile = new File(getServletContext().getRealPath("AvtoSolaZaposleniXSL.xsl"));      
    ExampleXML2PDF.generirajPDF(xmlfile, xsltfile);

    //Počakaj da se v miru zgenerira PDF
    try {
        Thread.sleep(5000L);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    }

    //Zaženi pdf
    File f1 = new File(getServletContext().getRealPath("Avtosola.pdf"));
    String pdfKoncni = f1.toString();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfReader reader = new PdfReader(pdfKoncni);
    PdfStamper stamper = null;
    try {
        stamper = new PdfStamper(reader, baos);
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    try {
        stamper.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    // set some response headers
    response.setHeader("Expires", "0");
    response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
    response.setHeader("Pragma", "public");
    response.setContentType("application/pdf");
    response.setContentLength(baos.size());

    OutputStream os = response.getOutputStream();
    baos.writeTo(os);
    os.flush();
    os.close();
}

我的所有文件都在WebContent文件夹中,而我的Servlet在默认包中。

错误:

(错误位置未知)java.io.FileNotFoundException:C:\ Eclipse \ eclipse \ WebContent \ AvtoSolaZaposleniXSL.xsl(系统找不到指定的文件) 显示java.lang.NullPointerException

相信我,我已经找到了这样的答案,并没有找到任何可以帮助我的东西。即使我把整个路径(不是C:\ Eclipse \ eclipse ......而且我不知道为什么会这样说......)它仍然无效。

像我说的那样。如果我在普通的Java类中运行它,它会正常生成PDF并且工作得很好......

import java.io.File;

public class Test {

public static void main(String[] args) {        

    File xmlfile = new File("WebContent/AvtoSolaZ.xml");
    File xsltfile = new File("WebContent/AvtoSolaZaposleniXSL.xsl");
    ExampleXML2PDF.generirajPDF(xmlfile, xsltfile);
}
}

请帮忙!

2 个答案:

答案 0 :(得分:0)

服务器引用读取文件的绝对路径。

所以在你的情况下..把你的文件放在projectRootName / AvtoSolaZaposleniXSL.xsl下。

它会起作用。

答案 1 :(得分:0)

我重新思考了我的问题,并提出了一个更简单的解决方案,用于显示生成的pdf。我刚刚使用了我的类,它生成了一个pdf(XSL-FO转换,需要xml和xsl ......)并将代码复制到servlet中。调整了输出流和voula!

这是新servlet的代码,完全正常:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    try {

        response.setContentType("application/pdf"); 

        // Setup directories
        File xmlfile = new File(getServletContext().getRealPath("/AvtoSolaZ.xml"));
        File xsltfile = new File(getServletContext().getRealPath("/AvtoSolaZaposleniXSL.xsl")); 
        File outDir = new File("WebContent");
        outDir.mkdirs();

        // configure fopFactory as desired
        FopFactory fopFactory = FopFactory.newInstance();

        FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
        // configure foUserAgent as desired

        // Setup output
        OutputStream out = response.getOutputStream();

        try {
            // Construct fop with desired output format
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

            // Setup XSLT
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory
                    .newTransformer(new StreamSource(xsltfile));

            // Set the value of a <param> in the stylesheet
            transformer.setParameter("versionParam", "2.0");

            // Setup input for XSLT transformation
            Source src = new StreamSource(xmlfile);

            // Resulting SAX events (the generated FO) must be piped through
            // to FOP
            Result res = new SAXResult(fop.getDefaultHandler());

            // Start XSLT transformation and FOP processing
            transformer.transform(src, res);

        } finally {
            out.close();
        }

    } catch (Exception e) {
        e.printStackTrace(System.err);
        System.exit(-1);
    }
}