简体   繁体   English

Java Web应用程序-文件读取

[英]Java Web Application - File Read

This question is related to " Java Web Application - File Upload ". 此问题与“ Java Web应用程序-文件上载 ”有关。
Is it possible for a Web Application to read a file from the host file system? Web应用程序是否可以从主机文件系统读取文件?
My guess is that for security reasons the answer is no - but I guess it should be possible for it to read files in directories below the root of the application. 我的猜测是,出于安全原因,答案是否定的-但我想它应该有可能读取应用程序根目录下目录中的文件。
If so, could someone supply an example code snippet. 如果是这样,有人可以提供示例代码段。
Thanks 谢谢

I'm using the Apache Wicket web-framework, deployed on the Glassfish 2 web-app container, and this snippet works fine for me: 我正在使用Apache Wicket Web框架,该框架已部署在Glassfish 2 Web应用程序容器上,此代码段对我来说效果很好:

//Read file content
FileInputStream in = new FileInputStream(new File("absolute path"));
byte[] bt = new byte[(int) file.length()];
in.read(bt);
in.close();
return bt;

Could however be related to the way the web application server is setup, so your mileage may vary.. 但是,可能与Web应用程序服务器的设置方式有关,因此您的工作量可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM