Java Web应用程序 - 使用Apache POI创建word文档会引发错误502

时间:2017-07-20 06:51:49

标签: java iis apache-poi

我目前正在使用java开发CGI Web应用程序并将其托管在IIS10-Webserver上。我遇到的问题如下:如果我使用Apache POI创建一个Word-Document并在我的计算机上本地运行该应用程序,我就没有问题创建该文件。但是当我尝试在我的网络服务器上托管应用程序时,我收到服务器错误502.我已经发现在创建文件时出现错误。我认为这是因为应用程序使用标准输出来创建它。所以问题是我在这种情况下没有响应标题,我也不想拥有响应标题。

public void writeReport(String[] params) {
    try {
        String url = "jdbc:mysql://localhost:3306/databasename";                
        MysqlDataSource dataSource = null;

        dataSource = new MysqlDataSource();
        dataSource.setUser("user");
        dataSource.setPassword("password");
        dataSource.setUrl(url);
        dataSource.setDatabaseName("databasename");

        this.connection = dataSource.getConnection();
        this.statement = this.connection.createStatement();

        this.resultSet = this.statement.executeQuery("select prename, lastname from users where username='"+params[1]+"'");

        String name = "";
        if(this.resultSet.next()) {
            name+=this.resultSet.getString("prename")+" "+this.resultSet.getString("lastname");
        }

        //Blank Document
        XWPFDocument document= new XWPFDocument(); //Here I get the Server Error 502

        //Headline
        XWPFParagraph headline = document.createParagraph();
        headline.setAlignment(ParagraphAlignment.CENTER);
        //Ausgeben
        XWPFRun headlineRun=headline.createRun();
        headlineRun=headline.createRun();
        headlineRun.setBold(true);
        headlineRun.setFontSize(20);
        headlineRun.setText("Ausgabezettel");
        headlineRun.addBreak();

        //datetime
        GregorianCalendar now = new GregorianCalendar();
        DateFormat dfDate = DateFormat.getDateInstance(DateFormat.MEDIUM);
        DateFormat dfTime = DateFormat.getTimeInstance(DateFormat.MEDIUM);

        //Informationen
        XWPFParagraph information = document.createParagraph();
        //Ausgeben
        XWPFRun informationRun=information.createRun();
        informationRun=information.createRun();
        informationRun.setText("Ausgegeben durch: "+name);
        informationRun.addBreak();
        informationRun.setText("Datum: "+dfDate.format(now.getTime()));
        informationRun.addBreak();
        informationRun.setText("Uhrzeit: "+dfTime.format(now.getTime()));
        informationRun.addBreak();


        //create table
        XWPFTable table = document.createTable();       
        CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9426));

        //create first row
        XWPFTableRow tableRowOne = table.getRow(0);
        tableRowOne.getCell(0).setText("Hersteller");
        tableRowOne.addNewTableCell().setText("Menge");
        tableRowOne.addNewTableCell().setText("Einheit");
        tableRowOne.addNewTableCell().setText("Artikelnummer");
        tableRowOne.addNewTableCell().setText("Lagerplatz");

        //Leerzeilen
        XWPFParagraph emptyLines = document.createParagraph();
        //Ausgeben
        XWPFRun emptyLinesRun=emptyLines.createRun();
        emptyLinesRun=emptyLines.createRun();
        emptyLinesRun.addBreak();
        emptyLinesRun.addBreak();
        emptyLinesRun.addBreak();
        emptyLinesRun.addBreak();


        //Unterschrift
        XWPFParagraph signature = document.createParagraph();
        signature.setBorderTop(Borders.SINGLE);
        signature.setAlignment(ParagraphAlignment.CENTER);
        //Ausgeben
        XWPFRun signatureRun=signature.createRun();
        signatureRun=signature.createRun();
        signatureRun.setText("Name                                                                       Datum                                                                       Unterschrift");

        // create footer
        XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();
        if (headerFooterPolicy == null) headerFooterPolicy = document.createHeaderFooterPolicy();
        XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

        XWPFParagraph footerParagraph = footer.createParagraph();
        footerParagraph.setAlignment(ParagraphAlignment.RIGHT);

        XWPFRun footerRun = footerParagraph.createRun();  
        footerRun.setText("Seite ");
        footerParagraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
        footerRun = footerParagraph.createRun();  
        footerRun.setText(" von ");
        footerParagraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");

        //Write the Document in file system
        String fileName = dfDate.format(now.getTime())+dfTime.format(now.getTime());
        fileName = fileName.replace(".", "");
        fileName = fileName.replace(":", "");
        fileName = fileName.replace(" ", "");
        FileOutputStream out = new FileOutputStream(new File(fileName+".docx"));

        document.write(out);
        out.close();
    } catch(Exception ex) {
        this.view.returnAnswer("Fehler bei Erstellung des Reports!");
    }
}

如果有人知道如何解决或绕过这个问题,那么请告诉我。任何帮助表示赞赏。抱歉我的英语不好;)

编辑: 使用来自javasript的ajax-request调用该应用程序,该javasript也在webserver(c:\ inetpub \ wwwroot)上。该应用程序是一个.jar文件,使用CgiModule处理。

0 个答案:

没有答案