访问EJB中的文件(Access files within EJB)

编程入门 行业动态 更新时间:2024-10-24 15:17:53
访问EJB中的文件(Access files within EJB)

根据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.

更多推荐

本文发布于:2023-07-22 19:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222756.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   EJB   files   Access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!