根据EJB规范,我是否允许在EJB中使用java.io和java.nio包来操作文件?
According to EJB specification, am I allowed to use java.io and java.nio packages within EJBs in order to manipulate files?
最满意答案
不,您不被允许这样做,因为如果应用程序将在某个时刻聚集,您永远不知道您的文件保存在哪个位置。 因此,Java EE环境中的文件操作是相当不安全的操作。
安全地执行此操作的方法可能是在JNDI中提供服务,这将关注您的对象序列化 - 请参阅示例
ByteArrayOutputStream baos= new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(myObj); new InitialContext().bind("path/to/FileManipulator", baos.toByteArray());有关进一步说明,请参阅此答案。 这里还有一篇文章描述了在EJB中使用java.io规范。
No, you are not allowed to do that because if the application will be clustered at some point, you never know to what location has your files been saved. So file manipulation in Java EE environment is rather unsafe operation.
The way to do this safely would be probably to have service in JNDI which will take care about your object serialization - see example
ByteArrayOutputStream baos= new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(myObj); new InitialContext().bind("path/to/FileManipulator", baos.toByteArray());See also this answer for further explanation. Also here is an article which describes what specification says about using java.io in EJB.
更多推荐
发布评论