Java Web应用程序-使用Apache POI创建word文档时抛出错误502
我目前正在用java开发一个CGI web应用程序,并将其托管在IIS10 web服务器上。我遇到的问题如下:如果我使用ApachePOI创建Word文档并在本地计算机上运行应用程序,那么创建该文件就没有问题。但是当我尝试在我的Web服务器上托管应用程序时,我得到一个服务器错误502。我已经发现在创建文件时出现了错误。我认为这是因为应用程序使用标准输出来创建它。所以问题是在这种情况下我没有响应头,我也不想有响应头Java Web应用程序-使用Apache POI创建word文档时抛出错误502,java,iis,apache-poi,Java,Iis,Apache Poi,我目前正在用java开发一个CGI web应用程序,并将其托管在IIS10 web服务器上。我遇到的问题如下:如果我使用ApachePOI创建Word文档并在本地计算机上运行应用程序,那么创建该文件就没有问题。但是当我尝试在我的Web服务器上托管应用程序时,我得到一个服务器错误502。我已经发现在创建文件时出现了错误。我认为这是因为应用程序使用标准输出来创建它。所以问题是在这种情况下我没有响应头,我也不想有响应头 public void writeReport(String[] params)
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!");
}
}
如果有人知道如何解决或绕过这个问题,请告诉我。感谢您的帮助。对不起,我的英语不好;)
编辑:
该应用程序通过来自同样位于Web服务器(c:\inetpub\wwwroot)上的javasript的ajax请求进行调用。该应用程序是一个.jar文件,由CgiModule处理。打印出我前面提到的例外情况,这段代码在本地运行得非常好,当我在IIS上托管应用程序时,我只收到一个服务器错误502。如果真的
XWPFDocument document=new XWPFDocument()代码>导致问题,那么它一定是内存问题,因为它不使用RAM
之外的资源。但你为什么认为这会导致问题?我怀疑FileOutputStream out=newfileoutputstream(新文件(fileName+“.docx”)由于文件名
只是一个没有任何目录路径的名称,因此会导致出现问题代码>尝试在没有写访问权限的地方进行写操作。如果您能够“在每个语句后面有一些输出”,那么首先您应该检查是否抛出了异常
。]catch(Exception ex){yourprint ex.getStackTrace().toString();…
其中,yourprint
是输出字符串的方法。因为如果new XWPFDocument()
真的失败,那么它应该抛出异常
“应用程序是一个用CgiModule处理的.jar文件。”如何运行?通过运行java-jar-jar文件
?如果是,那么类路径如何?当然,apache poi
类在类路径中?打印出我前面提到的例外情况,这段代码在本地运行得非常好,当我在IIS上托管应用程序时,我只会收到一个服务器错误502。如果真的XWPFDocument document=new XWPFDocument();
导致问题,那么它一定是内存问题,因为它不使用RAM
之外的资源。但是为什么您认为这会导致问题?我怀疑FileOutputStream out=new FileOutputStream(新文件名+“.docx”))
会导致这个问题,因为文件名
只是一个没有任何目录路径的名称,所以文档。write(out);
尝试在它没有写访问权限的地方写出来。如果你能够“在每个语句后面有一些输出”,那么首先你应该检查是否抛出了异常(Exception ex){yourprint ex.getStackTrace().toString();…
其中,yourprint
是输出字符串的方法。因为如果new XWPFDocument()
真的失败,那么它应该抛出一个Exception
“应用程序是一个用CgiModule处理的.jar文件。”如何运行?通过运行java-jar文件
?如果是,那么类路径如何?当然,apachepoi
类在类路径中?