您可以将 XLS 模板放入 java 包中,如下所示:
MyWebApp
+---src
| \---org
| \---paulvargas
| \---test
| | TestServlet.java
| |
| \---resources
| template.xls
|
\---WebContent
\---WEB-INF
| web.xml
|
\---lib
部署应用程序时,文件template.xls
会自动放在java包所指示的classes
目录中, . 因此,要读取文件:WEB-INF/classes/org/paulvargas/test/resources/
package org.paulvargas.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
InputStream inputStream = classLoader
.getResourceAsStream("org/paulvargas/test/resources/template.xls");
// Populate the template
}
}
也可以看看: