cancel
Showing results for 
Search instead for 
Did you mean: 

Code to establish a connection with ftp

former_member185881
Active Participant
0 Kudos

Hi All

Code to establish a connection with ftp and then delete the contents of file

Through this code i am trying to delete the contents of a file at FTP location

FileLocation = new URL("ftp://+username:password@IPAddress/Directory" +"/"+fileName+ "");
URLConnection uc = FileLocation.openConnection() ;  // to establish connection with FTP server using URL
uc.setDoOutput(true);
OutputStream out = uc.getOutputStream();
Audit.addAuditLogEntry(auditMKey,AuditLogStatus.SUCCESS,"CustomModule :: file"+out);
out.flush();//to make the file empty 
out.close();//close the file so that connection can be terminated

Output out is not getting any value (not getting initialized)

Please let me know the reason.

Regards

Dheeraj Kumar

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Dheeraj,

I think you just need to write something to the output stream. This worked for me:


URL url = new URL("ftp://<user>:<pass>@<host>:<port>/<pathToFile>");
URLConnection urlConn = url.openConnection() ;
urlConn.setDoOutput(true);
OutputStream out = urlConn.getOutputStream() ;			    
out.write(new byte[0]);
out.flush();
out.close();

If you need to do more than this perhaps look at: http://commons.apache.org/net/

-Russ