How to get the path to the server folder inside the Java program (J2EE + JSTL)

I wanted to read the contents of Excel files in my web project (J2EE-JSP + Servlets), which are located inside the web server folder.

I created a java file that I will call through a JSP page using the JSTL library, but I need to get the path to the Excel worksheet in the Java file so that I can read the contents.

How can I get the path to the current java file and thus the excel file?

In addition, I will read the contents of the Excel file through the POI library. I was able to do this in J2SE development, but is this possible here?

POIFSFileSystem fs = null;
    try {
        fs = new POIFSFileSystem(new FileInputStream("**some path here of sheet**"));
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    HSSFWorkbook wb = null;
    try {
        wb = new HSSFWorkbook(fs);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row;
    HSSFCell cell1,cell2,cell3; 

    int rows; // No of rows, stores the no. of rows for an excel sheet
    int cols; // No of columns, stores the no. of columns for an excel sheet

    rows = sheet.getPhysicalNumberOfRows();
    cols = 5;

    for(int r = 0; r < rows; r++) {
        row = sheet.getRow(r);

        if(row != null) {
            cell1 = row.getCell(1);
            cell2 = row.getCell(2);
            cell3 = row.getCell(4);

            //System.out.println(cell1.getStringCellValue()+" "+cell2.getStringCellValue()+" "+cell3.getStringCellValue());                
        }
    }
}
+3
source share
5 answers

You can query the servlet context for translation relative to the real path:

context.getRealPath("/");

Java-, -

getServletConfig().getServletContext();

, jsp- - , .

, :

public class AppStartup implements ServletContextListener {

    public void contextDestroyed(ServletContextEvent event) {
    }

    public void contextInitialized(ServletContextEvent event) {
        ServletContext context = event.getServletContext();
        // remember it in some static class for future use
    }
}

, , , , , , , jsp .

+3

JSP/Servlet , , JSP, ( JSTL, ). - Excel, , javax.servlet.http.HttpServlet , ServletContext

String physicalFolder = getContext().getRealPath("/");

- JSTL ( , < jsp: useBean/ > ), bean - - :

<jsp:useBean id="excelBean" scope="request" class="yourpackage.YourClass" >
    <jsp:setProperty name="physicalPath" value="<%= application.getRealPath("/") %>" />
</jsp:useBean> 
+1

.

, WEB-INF/classes getResourceAsStream, InputStream , .

.

+1

, , . - ( , Tomcat, ) , , , WAR. Excel, .

, , , - Excel JAR WEB-INF/lib,

getClass().getResourceAsStream("/class/path/to/file.xls")

new FileInputStream("/local/filesystem/path/file.xls")

getResourceAsStream ( "..." ) , . , Excel JAR, com.yourcompany.files,

getClass().getResourceAsStream("/com/yourcompany/files/file.xls")
0

duffymo Martin McNulty, getResourceAsStream Excel, :

webapp - - , ?

- Application Root
 |
 +- WEB-INF
     |
     +- classes/  <--- This is added to CLASSPATH
     |
     +- lib/      <--- Its contents are added to CLASSPATH
     |
     +- web.xml

JAR, lib . IDE , , .class , Java, , .

, ... , Excel, , .

Excel , , JAR WAR, .

The Class.getResourceAsStream (String path) method gives you access to files in the classpath, just pass the path replacing the dots with a slash, for example:

getClass().getResourceAsStream("/yourpackage/YourExcelFile.xsl");

will provide you with an InputStream of the YourExcelFile.xsl file, which is located inside yourpackage package.

I tried to explain this to you, but if in doubt, you can check the documentation.

0
source

Source: https://habr.com/ru/post/1709244/


All Articles