servlet中的文件读取问题
我必须使用 servlet 读取文件。这是我正在使用的代码。但是文件没有使用此代码读取。始终打印 File contains null value---------------- - :
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
response.setContentType("text/html");
String filename = "D/root.properties";
ServletContext context = getServletContext();
InputStream inp = context.getResourceAsStream(filename);
if (inp != null) {
InputStreamReader isr = new InputStreamReader(inp);
BufferedReader reader = new BufferedReader(isr);
PrintWriter pw = response.getWriter();
String text = "";
while ((text = reader.readLine()) != null) {
}
} else {
System.out.println("File contains null value-----------------");
}
} catch(Exception e) {
System.out.println("Rxpn............................................."+e);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
javadoc救援:
如果您想读取 Web 应用程序中的资源,请使用上面指定的路径。如果要从文件系统读取,请使用文件 IO(以及正确的文件名):
new FileInputStream("D:/root.properties")
javadoc to the rescue :
If you want to read from a resource in the web app, use a path as indicated above. If you want to read from the file system, use file IO (and the correct file name):
new FileInputStream("D:/root.properties")
使用以下代码。
这样你就可以读取文件
Use following code.
With this you can read file
请尝试上面的代码示例。我认为在您的代码中找不到文件。请给出上面代码中的文件路径并尝试。
Please try the above code sample. I think in your code , file is not found. Please give the file path in the above code and try.