Time: 14:23:19
Content: Implements FTP upload and download in JSP
Although FTP upload has been implemented, only English paths can be uploaded currently. This problem remains to be solved.
I. Required software:
FTP server: servusetup.exe: http://www.rhinosoft.com/__release/ServUSetup.exe
Java FTP library edtftpj/free: http://www.enterprisedt.com/products/edtftpj/download/edtftpj.zip
Ii. edtftpj/free provides the following functions:
1. Upload the specified folder (including sub-files and folders) uploadfolder (string Foldername, string ftppath)
2. Download the specified FTP folder downloadfolder (string ftppath, string outdir, string ftpdir)
3. Upload All files in the specified folder to the FTP specified directory uploadallfilesinfolder (string Foldername, string ftppath)
4. Delete all files in the specified folder (including files in subfolders, but the directory is not deleted due to unknown reasons) deleteallfilesinfolder (string ftppath, string ftpdir)
5. Delete the specified file string deletefile (string ftppath)
6. Determine whether the FTP directory contains isdirexist (string dirname, string [] files)
7. Upload a single file uploadfile (string clientfilename, string ftppath)
8. Download a single file downloadfile (string remotefilename, string localfilename)
9. Delete all objects in a specified folder (excluding subfolders, but deleting objects in a specified folder)
Iii. Implementation Process
0. Install the FTP server and configure the domain name, user name, password, and root directory.
1. Connect to the FTP server
/*************************************** ***********************************
The format for calling this constructor is ftpservice FTP = new ftpservice ("127.0.0.1", "21", "admin", "admin ");
If the print result is as follows, the connection to the server is successful.
Start logging on
Logon successful
Transferred to root directory
**************************************** **********************************/
/** <Br/> * initialize the connection <br/> * @ Param pftpserver FTP server address <br/> * @ Param pftpport FTP server port <br/> * @ Param pftpusername FTP logon username <br/> * @ Param pftppassword FTP logon password <br/> * @ throws ioexception <br/> */<br/> Public ftpservice (string pftpserver, string pftpport, string pftpusername, string pftppassword) throws exception <br/>{< br/> This. ftpserver = pftpserver; <br/> If (pftpport. trim (). equals ("") <br/> This. ftpport = "21"; <br/> else <br/> This. ftpport = pftpport; <br/> If (pftpusername. trim (). equals ("") <br/> This. ftpusername = "anonymous"; <br/> else <br/> This. ftpusername = pftpusername; <br/> This. ftppassword = pftppassword; <br/> try {<br/> ftpclient = new ftpclient (); // ftpserver, integer. parseint (ftpport) <br/> ftpclient. setremotehost (ftpserver); <br/> ftpclient. setremoteport (integer. parseint (ftpport); <br/> ftpclient. setcontrolencoding ("GBK"); // after adding this sentence, you can upload the Chinese file name in edtftpj 2.0.1. <br/> system. out. println ("start logging on"); <br/> ftpclient. connect (); <br/> ftpclient. login (ftpusername, ftppassword); <br/> system. out. println ("Logon successful"); <br/> ftpclient. chdir ("//"); // an error occurs when running on some FTP servers. Use ftpclient. chdir ("/") again <br/> system. out. println ("transferred to root directory"); <br/> islogin = true; <br/>}catch (exception e) {<br/> throw new exception (E. getmessage (); <br/>}< br/>}
2. Implement the file upload function: Upload All files in the specified folder to the specified FTP directory.
/** <Br/> * upload all files in the specified folder to the FTP directory <br/> * @ Param Foldername full path of the local folder to be uploaded <br/> * @ Param ftppath the path to the root directory on FTP <br/> * @ throws ioexception <br/> */<br/> Public void uploadallfilesinfolder (string Foldername, string ftppath) throws <br/> exception {<br/> If (islogin) {<br/> string strmsg = ""; <br/> try {<br/> file = new file (Foldername); <br/> If (file. isdirectory () {<br/> ftpclient. chdir ("//"); <Br/> ftpclient. settype (ftptransfertype. binary); <br/> If (checkfolderisexist (ftppath) {<br/> ftpclient. chdir (ftppath); <br/>}else {<br/> createfolder (ftppath); <br/>}< br/> file [] files = file. listfiles (); <br/> for (INT I = 0; I <files. length; I ++) {<br/> If (files [I]. isfile () {<br/> try {<br/> ftpclient. put (files [I]. getpath (), <br/> files [I]. getname (); <br/>} catch (effectio N ee) {<br/> strmsg + = "Upload File <:" + files [I]. getpath () + <br/> "> error! Message: "+ ee. getmessage () + <br/> "/R/N "; <br/>}< br/>} else {<br/> throw new exception (Foldername + "is not a folder 'name! "); <Br/>}< br/>} catch (exception e) {<br/> throw new exception (E. getmessage (); <br/>}< br/>}else {<br/> throw new exception ("You didnot login remote FTP server! "); <Br/>}< br/>
3. Implement the function of uploading a single file
/** <Br/> * upload a single file uploadfile (string clientfilename, string ftppath) <br/> * @ Param clientfilename full path of the local file to be uploaded <br/> * @ Param ftppath FTP path to the root directory <br/> * @ throws ioexception <br/> */<br/> Public void uploadfile (string clientfilename, string ftppath) throws <br/> exception {<br/> If (islogin) {<br/> try {<br/> // obtain the file name <br/> string filename = ""; <br/> int Index = clientfilename. lastindexof ("// "); <Br/> filename = clientfilename. substring (index + 1); <br/> ftpclient. chdir ("//"); <br/> ftpclient. settype (ftptransfertype. binary); <br/> If (checkfolderisexist (ftppath) {<br/> ftpclient. chdir (ftppath); <br/>}else {<br/> createfolder (ftppath); <br/>}< br/> ftpclient. put (clientfilename, filename); <br/>} catch (exception ex) {<br/> throw new exception (ex. getmessage (); <br/>}< br/>} e LSE {<br/> throw new exception ("You didnot login remote FTP server! "); <Br/>}< br/>
4. download the files and subfolders under the specified files on the FTP server
/** <Br/> * download the files and subfolders in the specified files on the FTP server <br/> * @ Param ftpdir the folder to be downloaded, relative Path starting from the root directory (excluding the name of the folder to be downloaded) <br/> * @ Param outdir: local directory of the file to be stored <br/> * @ Param ftpdir: folder to be downloaded <br/> * @ throws ioexception <br/> */ <br/> Public void downloadfolder (string ftppath, string outdir, string ftpdir) throws <br/> ioexception {<br/> try {<br/> ftppath = ftppath. replace ('//', '/'); <br/> If (! Ftppath. endswith ("/") {<br/> ftppath = ftppath + "/"; <br/>}< br/> outdir = outdir. replace ('//', '/'); <br/> If (! Outdir. endswith ("/") {<br/> outdir = outdir + "/"; <br/>}< br/> system. out. println ("current FTP path:" + file. separator + ftppath + ftpdir); <br/> system. out. println ("current local path:" + outdir); <br/> ftpclient. chdir (file. separator + ftppath + ftpdir); // enter the directory <br/> // ftpclient. chdir (ftppath); // enter the specified directory <br/> // list all files and folders in the directory <br/> ftpfile [] ftpfiles = ftpclient. dirdetails (""); </P> <p> for (INT I = 0; I <ftpfiles. l Ength; I ++) {<br/> ftpfile ftpobj = ftpfiles [I]; <br/> // system. out. println ("==================================== "); <br/> // system. out. println ("file name:" + ftpobj. getname (); <br/> // system. out. println ("Path:" + ftpobj. getpath (); <br/> // system. out. println ("whether it is a stop:" + ftpobj. isdir (); <br/> // system. out. println ("Last modified log:" + ftpobj. lastmodified (); <br/> // system. out. println ("======================================== "); <Br/> If (! Ftpobj. isdir () {// The file is downloaded directly <br/> system. out. println ("xiazaiwenjian:" + outdir + <br/> file. separator + ftpobj. getname (); <br/> // system. out. println (""); <br/> ftpclient. get (outdir + file. separator + ftpobj. getname (), <br/> ftpobj. getpath () + file. separator + <br/> ftpobj. getname (); <br/>}else {<br/> createdirectory (outdir, ftpobj. getname (); <br/> system. out. println (ftpobj. getpath (); <br/> downloadfolder (ftppath, <br/> outdir + file. separator + ftpobj. getname (), <br/> ftpdir + file. separator + ftpobj. getname (); <br/>}< br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p>
5. Download a single file
/** <Br/> * download a single file downloadfile (string remotefilename, string localfilename) <br/> * @ Param remotefilename -- file name on the server (including full path) <br/> * @ Param localfilename -- local file name (full path name) <br/> */<br/> Public void downloadfile (string remotefilename, string localfilename) {<br/> try {<br/> ftpclient. get (localfilename, remotefilename); <br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}finally {<br/>}< br/>}
6. delete a specified file
/** <Br/> * Delete the specified file string deletefile (string ftppath) <br/> * @ Param ftppath FTP root directory path <br/> * @ throws ioexception <br/> */<br/> Public String deletefile (string ftppath) throws <br/> exception {<br/> If (islogin) {<br/> string strmsg = ""; <br/> try {<br/> ftpclient. delete (ftppath); <br/> strmsg + = "File deleted successfully"; <br/>} catch (ftpexception ex) {<br/> strmsg + = "File Deletion error:" + ex. getmessage (); <br/>} Catch (ioexception ex) {<br/> strmsg + = "Operation file error:" + ex. getmessage (); <br/>}< br/> return strmsg; <br/>}else {<br/> throw new exception ("You didnot login remote FTP server! "); <Br/>}< br/>}
7. Delete the specified directory (including the folder itself)
/** <Br/> * 4. Delete the specified directory (including the folder itself) deletefolder (string ftppath) <br/> * @ Param ftppath FTP root directory path <br/> * @ throws ioexception <br/> */<br/> Public String deletefolder (string ftppath) throws <br/> exception {<br/> If (islogin) {<br/> string strmsg = ""; <br/> ftpclient. chdir ("//"); // enter the directory <br/> // list all files and folders in the directory <br/> ftpfile [] ftpfiles = ftpclient. dirdetails (ftppath); <br/> for (INT I = 0; I <ftpfile S. length; I ++) {<br/> ftpfile tempftpfile = ftpfiles [I]; <br/> system. out. println ("==================================== "); <br/> system. out. println ("file name:" + tempftpfile. getname (); <br/> system. out. println ("Path:" + tempftpfile. getpath (); <br/> system. out. println ("whether it is a stop:" + tempftpfile. isdir (); <br/> system. out. println ("Last modified log:" + tempftpfile. lastmodified (); <br/> system. out. println ("============================= ================ "); </P> <p> If (tempftpfile. isdir () {<br/> ftppath = ftppath + "//" + tempftpfile. getname (); <br/> system. out. println ("1:" + ftppath); <br/> deletefolder (ftppath); <br/>} else {<br/> ftpclient. delete (ftppath + "//" + tempftpfile. getname (); <br/>}< br/> ftpclient. cdup (); <br/> ftpclient. rmdir (ftppath); <br/> return strmsg; <br/>}else {<br/> throw new exception ("You di Dnot login remote FTP server! "); <Br/>}< br/>}
8. Other functions: Determine whether a folder or file exists; create a folder or file
/** <Br/> * determines whether a folder exists, this method is not very accurate <br/> * @ Param dirname the directory name to be determined <br/> * @ Param files FTP file list <br/> */<br/> public static Boolean isdirexist (string dirname, string [] files) {<br/> for (INT I = 0; I <files. length; I ++) {<br/> If (files [I]. indexof ("<dir>")>-1 & <br/> files [I]. indexof (dirname)>-1) {<br/> return true; <br/>}< br/> return false; <br/>}</P> <p>/** <br/> * create a directory on the FTP service <B R/> * @ Param directory the path to the root directory on FTP <br/> * @ Param subdirectory the directory to be created on FTP <br/> */<br/> private static void createdirectory (string directory, string subdirectory) {<br/> string dir []; <br/> file FL = new file (directory ); <br/> try {<br/> If (subdirectory = "" & FL. exists ()! = True) {<br/> Fl. mkdir (); <br/>} else if (subdirectory! = "") {<Br/> dir = subdirectory. replace ('//','/'). split ("/"); <br/> for (INT I = 0; I <dir. length; I ++) {<br/> file subfile = new file (directory + file. separator + dir [I]); <br/> If (subfile. exists () = false) {<br/> subfile. mkdir (); <br/>}< br/> directory + = file. separator + dir [I]; <br/>}< br/>} catch (exception ex) {<br/> system. out. println (ex. getmessage (); <br/>}< br/ >/** <Br/> * check whether the folder on the FTP server exists <br/> * @ Param pfolder specifies the path to the root directory on FTP. <br/> * @ throws exception <br/> */<br/> Public Boolean checkfolderisexist (string pfolder) throws exception {<br/> If (islogin) {<br/> string folder = pfolder. trim (); <br/> If (folder. startswith ("//") {<br/> folder = folder. substring (1); <br/>}< br/> If (folder. endswith ("//") {<br/> folder = folder. substring (0, folder. length ()-1); <br/>}< br/> string strlayer = ".. "; <br/> If (folder. indexof ("//")> 0) {<br/> string [] folders = folder. split ("//"); <br/> for (INT I = 1; I <folders. length; I ++) {<br/> strlayer + = ","; <br/> // system. out. println ("strlayer:" + strlayer); <br/>}< br/> boolean result = false; <br/> try {<br/> ftpclient. chdir (folder); <br/> ftpclient. chdir (strlayer); <br/> result = true; <br />} Catch (exception ex) {<br/> result = false; <br/>}< br/> return result; <br/>} else {<br/> throw new exception ("You didnot login remote FTP server! "); <Br/>}</P> <p>/** <br/> * Create a remote FTP server folder <br/> * @ Param pfolder FTP for the root directory path <br/> * @ throws exception <br/> */<br/> Public void createfolder (string pfolder) throws exception {<br/> If (islogin) {<br/> If (checkfolderisexist (pfolder) = false) {<br/> try {<br/> string Path = ""; <br/> ftpclient. chdir ("//"); <br/> string [] folders = pfolder. split ("//"); <br/> for (INT I = 0; I <F Olders. length; I ++) {<br/> try {<br/> ftpclient. chdir (folders [I]); <br/>} catch (exception ex) {<br/> ftpclient. mkdir (folders [I]); <br/> ftpclient. chdir (folders [I]); <br/>}< br/>} catch (exception ex) {<br/> throw new exception (ex. getmessage (); <br/>}< br/>}else {<br/> throw new exception ("You didnot login remote FTP server! "); <Br/>}< br/>}