2017-10-26 5 views
0

아래는 .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> 
,536,913,632 10

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); 
} 

web.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> 
+0

URL에/examples가있는 이유는 무엇입니까? – Nathan

+0

index.htmln에 도착하려고 할 때 어떤 URL을 누르십니까? http : // localhost : 8080/examples 또는 http : // localhost : 8080? – Nathan

+0

@ Nathan 서블릿의 .class 파일을 apache-tomcat-6.0.36 \ webapps \ examples \ WEB-INF \ classes 으로 복사하고 .html 파일을 으로 복사했습니다. apache-tomcat-6.0.36 \ webapps \ examples \ servlets \ ExcelWord_To_PDF_HTML 경로. 그래서 나는 \ examples을 사용 중입니다. –

답변

0

, 당신은 서블릿에서 양식 사용을 doPost() 방법을 게시 할 때,하지 doGet(). 그 후에 URL에서 /example을 제거하십시오.

+0

doPost()에서 doGet()을 호출하려고합니다. 삭제 시도/예,하지만 그것은 잘 작동하지 않습니다 .--( –