从java Web应用程序访问linux本地文件系统 [英] Accessing linux local file system from java web application

查看:372
本文介绍了从java Web应用程序访问linux本地文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在web应用程序中使用了一个java库,该应用程序托管在apache server.ReadConfFile方法库中,返回文件未找到错误。方法如下

we are using a java library inside a web application which is hosted in apache server.ReadConfFile method in library returns file not found error.The method is as follows

public byte[] ReadConfFile()
{
    try
    {
        File file = new File("/home/product/api/conf.txt");
        if(!file.exists())
            return "file not found".getBytes();
        byte[] buf = new byte[(int) file.length()];

        FileInputStream fis = new FileInputStream(file);
        fis.read(buf);
        return buf;
    } catch (IOException e)
    {
        e.printStackTrace();
        return null;
    }
}

是否允许从Web应用程序访问本地文件系统?如果是,那么是否有必须设置的访问权限?

Is local file system access from web application allowed?. If yes , then is there any access permission that has to be set?

推荐答案

快速回答您的问题:您可以访问来自Web应用程序的文件系统,但您必须检查应用程序服务器/ Web容器有关如何配置SecurityManager(如果已安装)。

To quickly answer your question: You can access the file system from a web application, but you would have to check your application server / web container on how to configure the SecurityManager (if one is installed).

但是,您的阅读文件的方法有严重的问题你应该说:

However, your method of reading the file has severe issues which you should adress:


  1. 不要检查 if(! file.exists())更好地检查 if(!file.isFile())。如果文件是目录,则第一次检查也返回true。

  1. Do not check if(!file.exists()) better check if(!file.isFile()). The first check also returns true if the file is a directory.

如果没有文件,最好不要返回String,抛出异常或加载一些默认配置或执行其他有用的操作。

If there is not a file, better not return a String, throw an Exception, or load some default Configuration or do something else which is useful.

您读取文件的逻辑非常糟糕。如果读取函数返回不同数量的可用字节(可能是读取的块),则会导致损坏结果。

Your logic for reading the file is very bad. If the read function returns a different amount of available bytes (maybe the read is chunked), you'll get a corrupt result.

您没有关闭在finally块中流,(或者如果使用Java 7则使用资源尝试)。

You do not close the stream in a finally block, (or use a try with resources if you are using Java 7).

异常处理也不好。打印堆栈跟踪不好,记录它会更好。处理异常将是最好的(抛出异常,或切换到某些默认配置,如文件不存在时)。

The exception handling is also not good. Printing the stacktrace is not good, logging it would be better. Handling the exception would be best (throw an exception, or switch to some default configuration, like when the file was not there).

编辑:如果要访问客户端的文件系统,则无法直接从服务器上运行的Web应用程序进行此操作。当然,这必须通过在客户端上运行的代码来完成,并且您必须填写有关客户端上运行的内容的更多详细信息,因为标准Web应用程序将在客户端上具有Javascript和(X)HTML ,而不是java。

If you want to access the client's file system, then this cannot be done from your web application running on the server directly. This of course would have to be done from code running on the client and you would have to fill in more details on what is running on the client side, since a "standard" web application would have Javascript and (X)HTML on the client, not java.

这篇关于从java Web应用程序访问linux本地文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆