我正在创建一个word文档,其中的数据来自Access数据库。我想创建页眉和页脚部分,并想在其中放置图像。但是我得到了这个错误。
java.io.FileNotFoundException: resources\images\page1_header.png (The system cannot find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at access_read_and_write.access_connection.start_page_1(access_connection.java:387)
at access_read_and_write.access_connection.Start(access_connection.java:161)
at Servlet.shop_visit_report.doGet(shop_visit_report.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:660)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:808)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
我的代码喜欢这样
public static void start_page_1(String SalesOrder) throws InvalidFormatException, IOException
{
XWPFHeader header = document.createHeader(HeaderFooterType.FIRST);
XWPFHeaderFooterPolicy headerpol = document.createHeaderFooterPolicy();
XWPFTable headerpictable= header.createTable(0,0);
headerpictable.removeBorders();
headerpictable.setTableAlignment(TableRowAlign.CENTER);
XWPFTableRow row5;
XWPFTableCell cell5;
CTDrawing drawing;
CTAnchor anchor;
row5=headerpictable.getRow(0);
cell5=row5.getCell(0);
headerpictable.setCellMargins(0, 0, 0, 500);
headerpictable.setTableAlignment(TableRowAlign.LEFT);
headerpictable.setWidthType(TableWidthType.PCT);
CTTrPr trPr;
CTHeight ht;
CTTblWidth cellwidth;
CTGroup ctGroup = CTGroup.Factory.newInstance();
XWPFParagraph paragpic=document.createParagraph();
paragpic.setSpacingBetween(0);
paragpic=cell5.getParagraphArray(0);
XWPFRun r1=paragpic.createRun();
/* File file = new File(imgFile);
InputStream pic = new FileInputStream(file);
InputStream pic = new BufferedInputStream(new FileInputStream(imgFile));
FileReader fileReader = new FileReader(imgFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
InputStream pic = new FileInputStream(imgFile);
InputStream pic = access_connection.class.getResourceAsStream(imgFile);
InputStream pic = new URL(imgFile).openStream();
r1.addPicture(pic, XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(200), Units.toEMU(90)); */
XWPFTable imagetalbe = document.createTable(0,0);
CTTblLayoutType type1 = imagetalbe.getCTTbl().getTblPr().addNewTblLayout();
type1.setType(STTblLayoutType.FIXED);
CTTblLayoutType type3 = headerpictable.getCTTbl().getTblPr().addNewTblLayout();
type3.setType(STTblLayoutType.FIXED);
imagetalbe.setTopBorder(XWPFBorderType.THREE_D_ENGRAVE, 10, 0, "C6DCF0");
imagetalbe.setBottomBorder(XWPFBorderType.THREE_D_ENGRAVE, 10, 0, "C6DCF0");
imagetalbe.setLeftBorder(XWPFBorderType.THREE_D_ENGRAVE, 10, 0, "C6DCF0");
imagetalbe.setRightBorder(XWPFBorderType.THREE_D_ENGRAVE, 10, 0, "C6DCF0");
XWPFParagraph parag = document.createParagraph();
XWPFTableRow row2;
XWPFTableCell cell2;
row2=imagetalbe.getRow(0);
cell2=row2.getCell(0);
parag=cell2.addParagraph();
parag.setAlignment(ParagraphAlignment.LEFT);
parag.setVerticalAlignment(TextAlignment. AUTO);
XWPFRun runimagetable = parag.createRun();
cell2.setColor("FFFFFF");
cell2.getCTTc().getTcPr().addNewGridSpan();
cell2.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf((short)25));
trPr = row2.getCtRow().addNewTrPr();
ht = trPr.addNewTrHeight();
ht.setVal(BigInteger.valueOf(-5000));
try {
FileInputStream is2 = new FileInputStream(new File(imgFile2));
runimagetable.addPicture(is2, XWPFDocument.PICTURE_TYPE_JPEG, imgFile2, Units.toEMU(450), Units.toEMU(200));
parag.setAlignment(ParagraphAlignment.LEFT);
parag.setVerticalAlignment(TextAlignment.TOP);
XWPFParagraph parag2=document.createParagraph();
XWPFRun runn= parag2.createRun();
runn.setFontFamily("Calibri");
runn.setFontSize(45);
runn.setColor("0A719B");
runn.setShadow(true);
parag2.setAlignment(ParagraphAlignment.CENTER);
runn.setText("SHOP VISIT REPORT");
XWPFTable table2=document.createTable(0,0);
XWPFTable table1=document.createTable(11,4);
table2.removeBorders();
table2.setLeftBorder(XWPFBorderType.THIN_THICK_THIN_MEDIUM_GAP, 20, 0, "090A09");
table2.setRightBorder(XWPFBorderType.THIN_THICK_THIN_MEDIUM_GAP, 20, 0, "090A09");
table2.setTopBorder(XWPFBorderType.THIN_THICK_THIN_MEDIUM_GAP, 20,0 , "090A09");
table1.removeInsideHBorder();
table1.removeInsideVBorder();
CTTblLayoutType type,type2;
table1.setTableAlignment(TableRowAlign.CENTER);
type = table1.getCTTbl().getTblPr().addNewTblLayout();
type.setType(STTblLayoutType.FIXED);
table2.setTableAlignment(TableRowAlign.CENTER);
type2 = table2.getCTTbl().getTblPr().addNewTblLayout();
type.setType(STTblLayoutType.FIXED);
XWPFTableRow row;
XWPFTableCell cell;
parag=document.createParagraph();
CTTrPr trPr2;
CTHeight ht2;
row=table2.getRow(0);
cell=row.getCell(0);
parag=cell.getParagraphArray(0);
XWPFRun run = parag.createRun();
cell.setColor("BCDFD5");
cell.getCTTc().getTcPr().addNewGridSpan();
cell.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf((short)6));
trPr2 = row.getCtRow().addNewTrPr();
ht2 = trPr2.addNewTrHeight();
ht2.setVal(BigInteger.valueOf((-800)));
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText("ENGINE INFORMATION");
run.setFontSize(20);
run.setFontFamily("Calibri");
run.setBold(true);
parag.setVerticalAlignment(TextAlignment.CENTER);
parag.setAlignment(ParagraphAlignment.CENTER);
row=table1.getRow(0);
trPr2 = row.getCtRow().addNewTrPr();
ht2 = trPr2.addNewTrHeight();
ht2.setVal(BigInteger.valueOf((-300)));
cell=row.getCell(0);
cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2500));
parag= cell.getParagraphArray(0);
run = parag.createRun();
cell.setText(" Customer");
cell=row.getCell(1);
cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2500));
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":");
CTSdtRun ctSdtRun = parag.getCTP().addNewSdt();
CTSdtComboBox ctSdtComboBox = ctSdtRun.addNewSdtPr().addNewComboBox();
CTSdtListItem ctSdtListItem = ctSdtComboBox.addNewListItem();
ctSdtListItem.setDisplayText("please select an option");
ctSdtListItem.setValue("please select an option");
ctSdtListItem = ctSdtComboBox.addNewListItem();
ctSdtListItem.setDisplayText("one");
ctSdtListItem.setValue("one");
ctSdtRun.addNewSdtContent().addNewR().addNewT().setStringValue("choose an item.");
cell=row.getCell(2);
cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2500));
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText("MODEL");
cell=row.getCell(3);
cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":V2527-AQ03");
row=table1.getRow(1);
trPr2 = row.getCtRow().addNewTrPr();
ht2 = trPr2.addNewTrHeight();
ht2.setVal(BigInteger.valueOf((-300)));
cell=row.getCell(0);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(" Engine S/N");
cell=row.getCell(1);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":V12492");
cell=row.getCell(2);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText("S/O NO");
cell=row.getCell(3);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":"+ SalesOrder);
row=table1.getRow(2);
trPr2 = row.getCtRow().addNewTrPr();
ht2 = trPr2.addNewTrHeight();
ht2.setVal(BigInteger.valueOf((-300)));
cell=row.getCell(0);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(" A/C Reg.");
cell=row.getCell(1);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":");
cell=row.getCell(2);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText("A/C Type");
cell=row.getCell(3);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":A320 Family");
row=table1.getRow(3);
trPr2 = row.getCtRow().addNewTrPr();
ht2 = trPr2.addNewTrHeight();
ht2.setVal(BigInteger.valueOf((-300)));
cell=row.getCell(0);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(" TSN");
cell=row.getCell(1);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":999999");
cell=row.getCell(2);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText("CSN");
cell=row.getCell(3);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":999999");
row=table1.getRow(4);
trPr2 = row.getCtRow().addNewTrPr();
ht2 = trPr2.addNewTrHeight();
ht2.setVal(BigInteger.valueOf((-300)));
cell=row.getCell(0);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(" TSLSV");
cell=row.getCell(1);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":999999");
cell=row.getCell(2);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText("CSLSV");
cell=row.getCell(3);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":999999");
row=table1.getRow(6);
trPr2 = row.getCtRow().addNewTrPr();
ht2 = trPr2.addNewTrHeight();
ht2.setVal(BigInteger.valueOf((-300)));
cell=row.getCell(0);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(" Reason for Removal");
cell=row.getCell(1);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":Over Inspection Per ESEO");
row=table1.getRow(7);
trPr2 = row.getCtRow().addNewTrPr();
ht2 = trPr2.addNewTrHeight();
ht2.setVal(BigInteger.valueOf((-300)));
cell=row.getCell(0);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(" Induction Date");
cell=row.getCell(1);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":");
parag.getCTP().addNewFldSimple().setInstr("DATE \\@ \"yyyy-MM-dd\" \\* MERGEFORMAT");
row=table1.getRow(9);
trPr2 = row.getCtRow().addNewTrPr();
ht2 = trPr2.addNewTrHeight();
ht2.setVal(BigInteger.valueOf((-300)));
cell=row.getCell(0);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText( " Prepared By");
cell=row.getCell(1);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":");
CTSdtRun ctSdtRun2 = parag.getCTP().addNewSdt();
CTSdtComboBox ctSdtComboBox2 = ctSdtRun2.addNewSdtPr().addNewComboBox();
CTSdtListItem ctSdtListItem2 = ctSdtComboBox2.addNewListItem();
ctSdtListItem2.setDisplayText("choose an item");
ctSdtListItem2.setValue("choose an item");
ctSdtListItem2 = ctSdtComboBox2.addNewListItem();
ctSdtListItem2.setDisplayText("one");
ctSdtListItem2.setValue("one");
ctSdtRun2.addNewSdtContent().addNewR().addNewT().setStringValue("choose an item.");
cell=row.getCell(2);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText("R.O No.");
cell=row.getCell(3);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":");
row=table1.getRow(10);
cell=row.getCell(0);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(" Prepared Date");
cell=row.getCell(1);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":");
parag.getCTP().addNewFldSimple().setInstr("DATE \\@ \"yyyy-MM-dd\" \\* MERGEFORMAT");
cell=row.getCell(2);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText("Rev. No.");
cell=row.getCell(3);
parag= cell.getParagraphArray(0);
run = parag.createRun();
run.setText(":");
for(int i =0;i<11;i++){
row=table1.getRow(i);
for(int j=0;j<4;j++)
{cell=row.getCell(j);
cell.setColor("BCDFD5");
}
}
table1.removeBorders();
CTTblPr tblpro = table1.getCTTbl().getTblPr();
CTTblBorders borders = tblpro.addNewTblBorders();
borders.addNewBottom().setVal(STBorder.THIN_THICK_THIN_MEDIUM_GAP);
borders.addNewLeft().setVal(STBorder.THIN_THICK_THIN_MEDIUM_GAP);
borders.addNewRight().setVal(STBorder.THIN_THICK_THIN_MEDIUM_GAP);
table1.getCTTbl().getTblPr().getTblBorders().getRight().setSz(BigInteger.valueOf(20));
table1.getCTTbl().getTblPr().getTblBorders().getLeft().setSz(BigInteger.valueOf(20));
table1.getCTTbl().getTblPr().getTblBorders().getBottom().setSz(BigInteger.valueOf(20));
FileOutputStream input = new FileOutputStream(word_saveas_file);
document.write(input);
input.close();
System.out.println("basarii");
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我的工作区文件夹中的图像。 WebContent->资源->图片-> page1_header.png这是我的图片路径。为什么我找不到此图片?