Writing to a file from an EJB

S

stevesuts

I am trying to write to a file using an EJB. I need to put the file on
the server for an FTP process. Does anyone have some example code that
will do that? Here is what I have so far, but I am having trouble...
public int writeToFile(String aosData) {
int returnCde = 0;

try
{
String dirName = ("c:\\AOSFTP");
String aosFileName = ("aosFtpFile.txt");
File aosFileDir = new File(dirName);
FileWriter fileWriter = new FileWriter(aosFileName);
BufferedWriter buffWriter = new BufferedWriter(fileWriter);


if(!aosFileDir.exists())
{
aosFileDir.mkdir();
}
else if(!aosFileDir.isDirectory())
{
System.out.println("The Directory does not exist");
returnCde = 1;
}
File aosFile = new File(dirName, aosFileName);
aosFile.createNewFile();

buffWriter.write(aosData);


buffWriter.flush();
buffWriter.close();



}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return (returnCde);
}

Any help is greatly appreciated.
 
K

kaeli

I am trying to write to a file using an EJB. I need to put the file on
the server for an FTP process. Does anyone have some example code that
will do that? Here is what I have so far, but I am having trouble...

What trouble?

Probably a permissions thing. What perms does the directory have?
This process runs as the web server, more than likely, and problems are often
the result of 755 perms or the like (only the owner has write perms).

--
 
S

stevesuts

Well, my problem is, there is nothing being written to the file. The
file gets created in the specified directory, but the string that I am
trying to write doesn't show up in the file. So, my permissions, I
think, are ok based on the fact that I can create the file. Can you
see what is wrong with my write statement. I don't recieve an error on
the write statement intself, so it thinks that it is writing to that
file. No errors that I can see.
 
A

Abrasive Sponge

Using File I/O in EJBs is a very, very, very bad idea!

Plus, app server containers adhere to the following permissions

Table 23 Java 2 Platform Security Policy for a Standard EJB Container
Permission name EJB Container policy
java.security.AllPermission deny
java.awt.AWTPermission deny
java.io.FilePermission deny
java.net.NetPermission deny
java.util.PropertyPermission grant “read”, “*”
deny all other
java.lang.reflect.ReflectPermission deny
java.lang.RuntimePermission grant “queuePrintJob”,
deny all other
java.lang.SecurityPermission deny
java.io.SerializablePermission deny
java.net.SocketPermission grant “connect”, “*” [Note A],
deny all other
Notes:
[A] This permission is necessary, for example, to allow enterprise beans
to use the client functionality of the
Java IDL and RMI-IIOP packages that are part of the Java 2 platform.


You would need to create a tiered system to have this run effectively


Standard Java App ---> Session Bean ----> Entity Beans -----> RDBMS

and in the standard java app is where you do your file io, but not in
any EJB.
 
S

stevesuts

I have read that you could use file i/o from within an ejb, so I
thought that I would try. Are you absolutely positive that you can't
do this, or are you theorizing? Sorry for questioning that, but I read
what I read on an IBM WSAD website, saying absolutely you can do that.
I am very new to all of this and I don't have a clue either way......
 
A

Abrasive Sponge

I have read that you could use file i/o from within an ejb, so I
thought that I would try. Are you absolutely positive that you can't
do this, or are you theorizing? Sorry for questioning that, but I read
what I read on an IBM WSAD website, saying absolutely you can do that.
I am very new to all of this and I don't have a clue either way......
Can I take a look at that link?
 
A

Abrasive Sponge

I have read that you could use file i/o from within an ejb, so I
thought that I would try. Are you absolutely positive that you can't
do this, or are you theorizing? Sorry for questioning that, but I read
what I read on an IBM WSAD website, saying absolutely you can do that.
I am very new to all of this and I don't have a clue either way......
Plus I got that permission table from the EJB-Spec itsself.
 
K

kaeli

public int writeToFile(String aosData) {

Do a system.err.println (or system.out -- whatever, just get a look at it) of
aosData to make sure it is not just an empty string and the problem is
actually here or in the process calling this method.

--
 
K

kaeli

Using File I/O in EJBs is a very, very, very bad idea!

Plus, app server containers adhere to the following permissions

Ooh, this is good to know.
I make my own beans, but I'm sure someday I'll need to use or at least know
about this aspect of EJBs.

I didn't realize they were so different from "normal" beans.

--
 
Joined
Jul 8, 2011
Messages
1
Reaction score
0
public static boolean writeToFile(String fileName, String dataLine,
boolean isAppendMode, boolean isNewLine) {
boolean returnCde=false;

DataOutputStream dos;
if (isNewLine) {
dataLine = "\n" + dataLine;
}
try {
File outFile = new File(fileName);
if (isAppendMode) {
dos = new DataOutputStream(new FileOutputStream(fileName, true));
} else {
dos = new DataOutputStream(new FileOutputStream(outFile));
}
dos.writeBytes(dataLine);
dos.close();
returnCde=true;
} catch (FileNotFoundException ex) {
returnCde=false;
} catch (IOException ex) {
returnCde=false;
}
System.out.println("Got a file to write in: "+fileName+" with data:\n"+dataLine+"\n exiting with code:"+returnCde);
return (returnCde);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top