I won’t say much nonsense, I will just post the code to you. The specific code is as follows:
//File upload public static boolean uploadToFTP(String url,int port,String username,String password,String path,String filename,InputStream input){ boolean success=false; FTPClient ftp=new FTPClient();//org.apache.commons.net.ftp try{ if(port>-1) { ftp.connect(url,port); }else{ ftp.connect(url);//ftp's default port is 21 } //Many people write to use ftp.getReplyCode() to get the return value of the connection, but this will cause the storeFileStream to return null if(ftp.login(username,password)) { ftp.enterLocalActiveMode(); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); //Create a directory, if it exists, it will fail to return ftp.makeDirectory(path); //Switch the directory ftp.changeWorkingDirectory(path); //Upload the file//FTP protocol stipulates that the file encoding format is ISO-8859-1 filename=new String(filename.getBytes("GBK"),"ISO-8859-1"); OutputStream out=ftp.storeFileStream(filename); byte[]byteArray=new byte[4096]; int read=0; while((read=input.read(byteArray))!=-1) { out.write(byteArray,0,read); } out.close(); ftp.logout(); success=true; } } catch(Exception e) { } finally{ if(ftp.isConnected()) { ftp.disConnecct(); } }}//File download public static boolean downloadFromFTP(String url,int port,String username,String password,String path,String localpath){ boolean success=false; FTPClient ftp=new FTPClient();//org.apache.commons.net.ftp try{ int reply; if(port>-1) { ftp.connect(url,port); }else{ ftp.connect(url);//ftp's default port is 21 } //Many people write to use ftp.getReplyCode() to get the return value of the connection, but this will cause the storeFileStream to return null ftp.login(username,password) ftp.enterLocalActiveMode(); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); reply=ftp.getReplyCode(); if(!FTPReply.isPositionCompletion(reply)) { ftp.disconnect(); return success;s } //Switch the directory here, if the switching fails, it means that there is no path on ftp.changeWorkingDirectory(path); //Upload the file FTPFile[]fs=ftp.listFiles(); OutputStream out=null; InputStream in=null; for(int i=0;i<fs.length;i++) { FTPFile ff=fs[i]; String outFileName=ff.getName(); //When creating a local file, you must convert the encoding format back String localFileName=new String(ff.getName().getBytes("ISO-8859-"),"GBK"); File localFile=new File(localpath+lcoalFileName); out=new FileOutputStream(localFile); in=ftp.retrieveFileStream(outFileName); byte[]byteArray=new byte[4096]; int read=0; while((read=in.read(byteArray))!=-1) { out.write(byteArray,0,read); } //This sentence is very important to operate the channel of the ftp stream multiple times, and wait for each command to complete ftp.completePendingCommand(); out.flush(); out.close(); ftp.logout(); sucess=true; } catch(Exception e) { } finally{ if(ftp.isConnected()) { ftp.disConnecct(); } }}The above is the Java implementation of ftp file upload and download that the editor introduces to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply you in time. Thank you very much for your support to Wulin.com website!