错误消息--->源服务器未找到目标资源的当前表示形式,或者不愿意透露存在



下面是将.xls/.xlsx文件转换为.pdf的代码

当我尝试点击链接时http://localhost:8080/examples/servlets/servlet/xlstopdfservlet

我收到以下错误 The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

ExcelWord2PDF_HTML.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XLS to PDF - Servlet Example</title>
</head>
<body >
<h1><a>HTML Form to Capture user Uploaded XLS File</a></h1>
<form id="form_533088" class="appnitro" enctype="multipart/form-data" 
method="post" action="/examples/servlets/servlet/xlstopdfservlet">
<ul >
<li id="li_1" >
<label for="element_1">Select an Excel File to Convert </label>
<input id="element_1" name="element_1" type="file"/> 
</li>
<li id="li_2" >
<label for="element_2">Excel Input Format </label>
<select class="element select medium" id="element_2" name="element_2"> 
<option value="" selected="selected"></option>
<option value="1" >XLS</option>
<option value="2" >XLSX</option>
</select>
</li>           
<li>                
<input id="saveForm" class="button_text" type="submit" name="submit" 
value="Submit" />
</li>
</ul>
</form> 
</body>
</html>

xlstopdfservlet.java

package com.infy.accelearte.excelword2pdf;

public class xlstopdfservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
        OutputStream out = response.getOutputStream();  
        try {
                //set InputStream object that accepts uploaded file
                InputStream filecontent=null;              
                List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                //assume default input type as XLS
                String inputtype="1"; 
                for (FileItem item : items) {
                        if (item.isFormField()) {                               
                                String fieldname = item.getFieldName();
                                if (fieldname.equals("element_2")) {
                                //identifies type of input required
                                inputtype = item.getString(); 
                                }
                                //the value can be 1 for XLS conversion, 2 for XLSX conversion
                        } else {
                        //The uploaded file is processed in this section
                        String fieldname = item.getFieldName();
                        String filename = FilenameUtils.getName(item.getName());
                        filecontent = item.getInputStream();
                        //Uploaded Excel file is obtained into Inputstream object at this step                
                        }
                }
         //we have obtained the user uploaded Excel file till this step.
         // we also know the output is a PDF.
         // we can now write the servlet code that converts the file.
         HSSFWorkbook my_xls_workbook=null;
         HSSFSheet my_worksheet=null;
         XSSFWorkbook my_xlsx_workbook=null;
         XSSFSheet my_worksheet_xlsx=null;
         //set the output format i.e. pdf         
         response.setContentType("application/pdf");
         Document document = new Document();
         PdfWriter.getInstance(document, out);
         document.open();
         //we create a PDF table that can hold XLS data
         //assumption is excel sheet is holding two columns only.
         //you can make this dynamic
         PdfPTable my_table = new PdfPTable(2);
         PdfPCell table_cell;
         //the object below loops through the excel document rows
         Iterator<Row> rowIterator = null;
         //read xls
         if (inputtype.equals("1")){
         my_xls_workbook = new HSSFWorkbook(filecontent); 
         my_worksheet = my_xls_workbook.getSheetAt(0);
         rowIterator=my_worksheet.iterator();    
         }
         //read xlsx
         if (inputtype.equals("2")){
         my_xlsx_workbook = new XSSFWorkbook(filecontent); 
         my_worksheet_xlsx = my_xlsx_workbook.getSheetAt(0);
         rowIterator=my_worksheet_xlsx.iterator();       
         }
         while(rowIterator.hasNext()) {
                        Row row = rowIterator.next(); 
                        Iterator<Cell> cellIterator = row.cellIterator();
                                while(cellIterator.hasNext()) {
                                        Cell cell = cellIterator.next(); //Fetch CELL
                                        switch(cell.getCellType()) { //Identify CELL type
                                        case Cell.CELL_TYPE_STRING:
                                                //Push the data from Excel to PDF Cell
                                                 table_cell=new PdfPCell(new Phrase(cell.getStringCellValue()));                                                
                                                 my_table.addCell(table_cell);
                                                break;
                                        }
                                        //next line
                                }
                }
        // add table to PDF document        
        document.add(my_table);     
        //close document..returns output to server
        document.close();
        }
        catch (Exception e) {
               System.err.println(e.toString()); /* Throw exceptions to log files */
         }
         finally {
                 out.close();/* Close the output stream */
         }
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    doGet(request, response);
}

网络.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ExcelWord_PDF</display-name>
  <welcome-file-list>
     <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
        <servlet-name>xlstopdfservlet</servlet-name>
        <servlet-class>com.infy.accelerate.excelword2pdf.xlstopdfservlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>xlstopdfservlet</servlet-name>
        <url-pattern>/servlets/servlet/xlstopdfservlet</url-pattern>
    </servlet-mapping>
</web-app>

首先,当您发布表单时,请在 servlet 中使用doPost()方法,而不是doGet() 。之后,尝试从您的网址中删除/example

最新更新