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

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

问题描述

我们在托管在apache服务器中的Web应用程序中使用java库.库中的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.

如果没有文件,最好不要返回字符串、抛出异常或加载一些默认配置或做其他有用的事情.

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,请对资源使用 try).

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天全站免登陆