Java uses ftpclient to upload image files

Source: Internet
Author: User

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 ;}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.