Nonsense not to say more, directly to everyone to paste the code, the specific code 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 default port is 21}//Many people write with Ftp.getreplycode () to get the return value of the connection, but this will cause Storefilestream to return Nu
ll if (Ftp.login (Username,password)) {Ftp.enterlocalactivemode ();
Ftp.setfiletype (Ftpclient.binary_file_type);
Create the directory, and if present will return the failure ftp.makedirectory (path);
Switch directory ftp.changeworkingdirectory (path);
Upload 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 ();
Sucess=true;
The 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,strin
G 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 default port is 21}//Many people write with Ftp.getreplycode () to get the return value of the connection, but this will cause 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}//Toggle directory Here you can tell that the failover is not on the FTP above the path ftp.changeworkingdirectory (path);
Upload 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 you create a local file, you turn back the encoding format. 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 is very important to operate this FTP stream more than once in the channel, to wait for his every command to complete ftp.completependingcommand ();
Out.flush ();
Out.close ();
Ftp.logout ();
Sucess=true;
The catch (Exception e) {} finally{if (ftp.isconnected ()) {Ftp.disconnecct (); } } }
The above is a small series of Java to introduce the implementation of FTP file upload download to solve the slow Chinese garbled file download and other issues, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!