First, name the file. tmp. After the upload is successful, modify the name.
Public static Boolean uploadimagebycommonftpse (File localimage, string imagerelativepath) {If (localimage = NULL |! Localimage. exists () {log. error ("local file does not exist"); Return false;} else if (imagerelativepath = NULL | "". equals (imagerelativepath) {log. fatal ("server path is blank"); Return false;} else {// The above code gets the FTP server information ftpclient = new ftpclient () from the configuration file (); try {ftpclient. connect (ftp_server_ip); Boolean loginstatus = ftpclient. login (ftp_server_user, ftp_server_password); If (loginstatus) {// set the uploaded file type to image ftpclient. setfiletype (ftpclient. bi Nary_file_type); // capture the last "/" occurrence location, as the FTP path string relativepath = ""; if (imagerelativepath. startswith ("/") {relativepath = imagerelativepath. substring (1, imagerelativepath. lastindexof ("/");} else {relativepath = imagerelativepath. substring (0, imagerelativepath. lastindexof ("/");} // capture the location where the last "/" appears. The content after this location is used as the file name string filename = imagerelativepath. substring (imagerelativepath. lastindexof ("/") + 1 ); // To prevent the file from being transmitted, the file is removed by the server program, resulting in blurred images. Therefore, when uploading a file, specify a temporary file name, format: official file name + ". TMP "string filenametemp = filename + ". TMP "; // recursively create a folder string [] mkds = relativepath. split ("/"); string curpath = ""; Boolean mkdflag = false; Boolean changedirectoryflag = false; For (INT I = 0; I <mkds. length; I ++) {If (mkds [I]! = NULL & mkds [I]! = "") {Mkdflag = ftpclient. makedirectory (mkds [I]); If (mkdflag) {log.info ("directory created successfully! ");} Else {log.info (" the directory already exists. ");} changedirectoryflag = ftpclient. changeworkingdirectory (mkds [I]); If (changedirectoryflag) {log.info ("Current Directory [" + mkds [I] + "]. ");} else {log. error ("failed to switch directory. ") ;}} log.info (" start uploading file ["+ filenametemp +"]. "); Boolean changestorefileflag = ftpclient. storefile (filenametemp, new fileinputstream (localimage); If (changestorefileflag) {ftpclient. rename (filenametemp, filename); log.info ("file [" + filenametemp + "] to [" + filename + "]. ");} else {log. error ("File Upload Failed. ");} Boolean logoutflag = ftpclient. logout (); Return true;} else {}} catch (socketexception E1) {log. error ("FTP server connection error", E1);} catch (ioexception E1) {log. error ("FTP server connection error", E1);} finally {try {ftpclient. disconnect ();} catch (ioexception e) {log. error (e) ;}}return false ;}}}