如何从EJB 3访问文件系统?

编程入门 行业动态 更新时间:2024-10-23 02:08:16
本文介绍了如何从EJB 3访问文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道如何从EJB 3 bean访问文件系统?

我搜索了关于这个主题的Internet并没有找到一个好的答案。

有些人建议使用java.io/java.nio,即使规范禁止这种用法。大多数应用程序服务器似乎都允许访问此API。

另一个想法是使用JCA连接器访问文件系统或LDAP目录。

>

我想这样做是为了避免在数据库中使用BLOB,当一个简单的文件在性能和使用的资源方面是一个更好的解决方案。 b $ b

你将如何解决这个问题?

解决方案

不允许从EJB中的文件系统访问,因为您无法控制应用程序在(Java EE) Container 中的运行方式。例如,您的应用程序可能会在一组服务器上运行,在这种情况下,将某个对象保存到一台服务器上的某个目录可能没有什么用处。 (当然你可能有一个网络文件系统,所以限制可能不适用)。

一个选项可能是使用 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(myObj); //现在保存到JNDI InitialContext()。bind(path / to / myobject,baos.toByteArray());

可以稍后查找并重新转换为您的对象:

byte [] bs =(byte [])new InitialContext()。lookup(path / to / myobject); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bs)); MyObj myObj =(MyObj)ois.readObject();

或者,您可以使用 java.beans 持久化XML (即 XMLDecoder , XMLEncoder )将您的实例编码为XML字符串保存到JNDI中。

I would like to know how can I access the file system from an EJB 3 bean?

I searched the Internet on the subject and haven't found a good answer.

Some suggest using the java.io/java.nio even though the specification prohibits this usage. Most application servers seem to allow the access to this API anyway.

Another idea would be to use an JCA connector to access the file system or a LDAP directory.

What I want to do this to avoid the use of BLOB in the database when a simple file would be a much better solution in terms of performance and used resources.

How would you solve this problem?

解决方案

The reason that you are disallowed from file system access in EJBs is that you have no control over how your application runs within a (Java EE) Container. For example, your application may be run across a cluster of servers, in which case saving some object to a directory on one server is likely to be of little use. (You may have a network file-system of course, so the restriction may not apply).

One option may be to use the JNDI implementation which comes with your Container. You will likely be able to save a raw byte[] array at some JNDI location, so you could always save down the serialized form of the object:

ByteArrayOutputStream baos= new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(myObj); //Now save into JNDI new InitialContext().bind("path/to/myobject", baos.toByteArray());

This can be looked up later and re-converted into your object:

byte[] bs = (byte[]) new InitialContext().lookup("path/to/myobject"); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bs)); MyObj myObj = (MyObj) ois.readObject();

Alternatively you could use the java.beans persistent XML (i.e. XMLDecoder, XMLEncoder) to encode your instance as an XML string an save that into JNDI.

更多推荐

如何从EJB 3访问文件系统?

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

发布评论

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

>www.elefans.com

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