如何从EJB 3访问文件系统? [英] How to access the file system from an EJB 3?

查看:135
本文介绍了如何从EJB 3访问文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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



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

>

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

你将如何解决这个问题?

解决方案

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

一个选项可能是使用 Container 自带的 JNDI 实现。您可能会在某个JNDI位置保存原始的 byte [] 数组,因此您可以始终保存该对象的序列化形式:

  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访问文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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