Java FTP: Can't delete local file after upload to FTP -
i'm not able delete local file after successful upload ftp (so goal delete local file right after upload done). suspects fileinputstream blocks local file i'm not sure. maybe there else can't see. fine. i'm using org.apache.commons.net.ftp.ftp. here part of code:
public class ftpconnection { string ftp_server; string ftp_user; string ftp_password; int ftp_port; public ftpclient ftpclient; int attempts = 3; long file_size; public ftpconnection(string ftp_server, string ftp_user, string ftp_password, int ftp_port) { ftp_server = ftp_server; ftp_user = ftp_user; ftp_password = ftp_password; ftp_port = ftp_port; ftpclient = new ftpclient(); ftpclient.enterlocalpassivemode(); ftpclient.setbuffersize(1024 * 1024 * 1024); ftpclient.setconnecttimeout(120000); ftpclient.setdatatimeout(60000); try { ftpclient.connect(ftp_server, ftp_port); int replycode = ftpclient.getreplycode(); if (!ftpreply.ispositivecompletion(replycode)) { system.out.println("operation failed. server reply code: " + replycode); return; } boolean success = ftpclient.login(ftp_user, ftp_password); ftpclient.setfiletype(ftp.binary_file_type); if (!success) { system.out.println("could not login server"); } } catch (ioexception ex) { system.out.println("oops! wrong happened"); ex.printstacktrace(); } } public boolean copyfile(file source, string destination) { try { file_size = source.length(); for(int i=0;i<attempts;i++) { fileinputstream file = new fileinputstream(source); ftpclient.storefile(destination, file); for(ftpfile f : ftpclient.listfiles()) { if(f.getname().equals(destination) && f.getsize() == source.length()) { file.close(); return true; } } } ftpclient.deletefile(destination + "/" + source.getname()); return false; } catch (ioexception ex) { logger.getlogger(ftpconnection.class.getname()).log(level.severe, null, ex); this.closeconnection(); return false; } } }
and here call copyfile method:
file f = new file("myfile.txt"); boolean b = ftp.copyfile(orig,filename); if(b) { system.out.println(f.exists()); //true system.out.println(f.canwrite()); //true system.out.println(f.delete()); //false }
Comments
Post a Comment