aspose file tools*
The moose likes Applets and the fly likes Update txt file on ftpserver via Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Update txt file on ftpserver via Applet" Watch "Update txt file on ftpserver via Applet" New topic
Author

Update txt file on ftpserver via Applet

emilio rossi
Ranch Hand

Joined: Jul 01, 2011
Posts: 47

Hello again, im trying to update a txt file which is in my ftp server public directory using apache commons net 3.0.1

so far im able to read the contend of this file and put it in a string:

public static String FTPstoreContact(String name,String mail,String mex){
String Report=("CONNESSIONE FTP: ");
try{
FTPClient client=new FTPClient();
client.connect(ftp,Integer.parseInt(port));
Report=Report+"OK\nLOGIN: ";
boolean lgn=client.login(ftpus,ftpass);
if(lgn==false){Report=Report+"NON RIUSCITO";}
else{Report=Report+"OK\n";
String remoteFile=("Contacts.txt");
InputStream in=client.retrieveFileStream(remoteFile);
BufferedReader rd=new BufferedReader(new InputStreamReader(in));
String rt=("");
StringBuilder build=new StringBuilder();
while((rt=rd.readLine())!= null){build.append(rt+"\n");}
String retrieved=build.toString();
retrieved=retrieved+(name+"\n"+mail+"\n"+mex+"\n");
rd.close();in.close();

now i want to write this new string to the same file, tryied storeFileStream but didnt get too far cause it is an Outputstream and not a FileOutputStream, and lookin around it seems there is no way to convert from one to another, maybe im approaching the problem from the wrong side...any tip???
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
You can't use FileOutputStream in an applet, but since this is about FTP, I don't think you need to. If you wrap an OutputStreamWriter and a BufferedWriter around the OutputStream you should have no problems writing the String to it.
emilio rossi
Ranch Hand

Joined: Jul 01, 2011
Posts: 47

hi , thanks for the reply
but i have no idea of how to do what you said
i can instantiate OutputStreamWriter and BufferedWriter to write a file on local machine, but dont know how to pass the string to the remote file using
client.someMethodIdontKnow()))
can you please post an example of what you mean with "wrappin them around" the OutputStream?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
new BufferedWriter(new OutputStreamWriter (OutputStream, ENCODING)).write(String)

The OSW constructor takes the encoding you want to use as parameter.
emilio rossi
Ranch Hand

Joined: Jul 01, 2011
Posts: 47

the outputstream looks unquestionably wrapped

thanks a lot for the post Ulf, im a self-made "javer" and have great lacks in java basics
emilio rossi
Ranch Hand

Joined: Jul 01, 2011
Posts: 47

...i tried :

OutputStream out=client.storeFileStream(remoteFile);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(out,"UTF-8"));
bw.write(retrieved);bw.flush();bw.close();out.close();
client.logout();client.disconnect();

but i get a null pointer exception on the second line...retrieved is surely not a null string, maybe the exception comes because of the storeFileStream method but i dont see any alternative in ftpclient documentation for writing on server file, and i dont want to create the file locally and then store it cause it will compel me to sign the application and i want it to operate totally serverside instead...any help?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
Start by finding out which object in which command is null.
emilio rossi
Ranch Hand

Joined: Jul 01, 2011
Posts: 47

the null exception is about the OutputStreamWriter, at
OutputStreamWriter os=new OutputStreamWriter(out,"UFT-8");
but out is ok, the encoding is ok...
after the exception i found the txt file on the server has been cleared, but the string i write on it is not a null one
emilio rossi
Ranch Hand

Joined: Jul 01, 2011
Posts: 47

ok i gave up the outputstreamwriter, now it is:

client.enterLocalPassiveMode();
OutputStream out=client.storeFileStream("TempContacts.txt");
System.out.println(client.getReplyString());
out.write(("write test").getBytes());
out.flush();out.close();
System.out.println(client.completePendingCommand());
System.out.println(client.getReplyString());

the file TempContact is created but it is empty, and i still get the nullpointerexception.
i read storefilestream can return null if there are file access conflicts but the previous inputstream is closed and im logged out from site control panel,
how comes???
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
This: "write test".getBytes() is not a good idea; you should always specify the encoding, especially when there are different computers involved. Relying on the platform default encoding is bound to cause problems.

What does client.getReplyString() return?

im logged out from site control panel

What control panel? What does that have to do with running this code?
emilio rossi
Ranch Hand

Joined: Jul 01, 2011
Posts: 47

i've added a "UTF-8"
i mean the control panel where you manually manage your site, i thought it would cause an access conflict so i logged out before try, but nevermind,
the problem was i forgot to add a completependingcommand after closing the inputstream so it somehow denied the outputstream access to the file, now everything works fine

just a last tip, writing the string byte i lose all the "\n" so instead of returning words are alltoghether, is there a way to return in byte?

thanks for the help
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
Try setting the transfer mode to BINARY instead of ASCII; that should leave the file alone.
emilio rossi
Ranch Hand

Joined: Jul 01, 2011
Posts: 47

doesnt work,

tryed "\n".getBytes() or "\r\n".getBytes() which seems to work for everyone online but not for me...

EDIT:
but it is not a "\n" encoding problem, since nor BufferedWriter.newLine() works on a server .txt
emilio rossi
Ranch Hand

Joined: Jul 01, 2011
Posts: 47

a waste of time so far...
i did the whole job to avoid making a signed applet, but when on browser the applet asks again for permissions...
dont know why since im not doing anything on local machine, the only file im editing is the one on server...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  70
The exception should tell you which method or class causes it.
emilio rossi
Ranch Hand

Joined: Jul 01, 2011
Posts: 47

is in the need for a signed applet, finally i understood that what i needed is a servlet , did it and works fine
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Update txt file on ftpserver via Applet