??????JAVA?????У??????????FTP????????????FTP???????????????????????????????????????jakarta commons?е?FTPClient????commons-net???У????????????????
?????????????
????????????????????????????
????/**
????*Description:??FTP????????????
????*@Version1.0 Jul 27??2008 4:31:09 PM by?????cuihongbao@d-heaven.com??????
????*@param url FTP??????hostname
????*@param port FTP?????????
????*@param username FTP??????
????*@param password FTP???????
????*@param path FTP????????????
????*@param filename?????FTP??????????????
????*@param input??????
????*@return???????true????????false
????*/
????publicstaticboolean uploadFile(String url??int port??String username??String password??String path??String filename??InputStream input){
????boolean success=false;
????FTPClient ftp=new FTPClient();
????try{
????int reply;
????ftp.connect(url??port);//????FTP??????
????//????????????????????ftp.connect(url)???????????FTP??????
????ftp.login(username??password);//???
????reply=ftp.getReplyCode();
????if(!FTPReply.isPositiveCompletion(reply)){
????ftp.disconnect();
????return success;
????}
????ftp.changeWorkingDirectory(path);
????ftp.storeFile(filename??input);
????
????input.close();
????ftp.logout();
????success=true;
????}catch(IOException e){
????e.printStackTrace();
????}finally{
????if(ftp.isConnected()){
????try{
????ftp.disconnect();
????}catch(IOException ioe){
????}
????}
????}
????return success;
????}<pre></pre>
????/**
????*Description:??FTP????????????
????*@Version1.0 Jul 27??2008 4:31:09 PM by?????cuihongbao@d-heaven.com??????
????*@param url FTP??????hostname
????*@param port FTP?????????
????*@param username FTP??????
????*@param password FTP???????
????*@param path FTP????????????
????*@param filename?????FTP??????????????
????*@param input??????
????*@return???????true????????false
????*/
????public static boolean uploadFile(String url??int port??String username??String password??String path??String filename??InputStream input){
????boolean success=false;
????FTPClient ftp=new FTPClient();
????try{
????int reply;
????ftp.connect(url??port);//????FTP??????
????//????????????????????ftp.connect(url)???????????FTP??????
????ftp.login(username??password);//???
????reply=ftp.getReplyCode();
????if(!FTPReply.isPositiveCompletion(reply)){
????ftp.disconnect();
????return success;
????}
????ftp.changeWorkingDirectory(path);
????ftp.storeFile(filename??input);   
????
????input.close();
????ftp.logout();
????success=true;
????}catch(IOException e){
????e.printStackTrace();
????}finally{
????if(ftp.isConnected()){
????try{
????ftp.disconnect();
????}catch(IOException ioe){
????}
????}
????}
????return success;
????}
????
????????????д????С?????
????1.??????????????FTP????????????????£?
????@Test
????publicvoid testUpLoadFromDisk(){
????try{
????FileInputStream in=new FileInputStream(new File("D:/test.txt"));
????boolean flag=uploadFile("127.0.0.1"??21??"test"??"test"??"D:/ftp"??"test.txt"??in);
????System.out.println(flag);
????}catch(FileNotFoundException e){
????e.printStackTrace();
????}
????}<pre></pre>
????@Test
????public void testUpLoadFromDisk(){
????try{
????FileInputStream in=new FileInputStream(new File("D:/test.txt"));
????boolean flag=uploadFile("127.0.0.1"??21??"test"??"test"??"D:/ftp"??"test.txt"??in);
????System.out.println(flag);
????}catch(FileNotFoundException e){
????e.printStackTrace();
????}
????}
????2.??FTP????????????????????????????????д?????????
????@Test
????publicvoid testUpLoadFromString(){
????try{
????InputStream input=new ByteArrayInputStream("test ftp".getBytes("utf-8"));
????boolean flag=uploadFile("127.0.0.1"??21??"test"??"test"??"D:/ftp"??"test.txt"??input);
????System.out.println(flag);
????}catch(UnsupportedEncodingException e){
????e.printStackTrace();
????}
????}<pre></pre>
????@Test
????public void testUpLoadFromString(){
????try{
????InputStream input=new ByteArrayInputStream("test ftp".getBytes("utf-8"));
????boolean flag=uploadFile("127.0.0.1"??21??"test"??"test"??"D:/ftp"??"test.txt"??input);
????System.out.println(flag);
????}catch(UnsupportedEncodingException e){
????e.printStackTrace();
????}
????}
???????????????
??????FTP?????????????????????????ο????£?
????/**
????*Description:??FTP?????????????
????*@Version1.0 Jul 27??2008 5:32:36 PM by?????cuihongbao@d-heaven.com??????
????*@param url FTP??????hostname
????*@param port FTP?????????
????*@param username FTP??????
????*@param password FTP???????
????*@param remotePath FTP????????????·??
????*@param fileName???????????
????*@param localPath??????浽?????·??
????*@return
????*/
????publicstaticboolean downFile(String url??int port??String username??String password??String remotePath??String fileName??String localPath){
????boolean success=false;
????FTPClient ftp=new FTPClient();
????try{
????int reply;
????ftp.connect(url??port);
????//????????????????????ftp.connect(url)???????????FTP??????
????ftp.login(username??password);//???
????reply=ftp.getReplyCode();
????if(!FTPReply.isPositiveCompletion(reply)){
????ftp.disconnect();
????return success;
????}
????ftp.changeWorkingDirectory(remotePath);//????FTP????????
????FTPFile[]fs=ftp.listFiles();
????for(FTPFile ff:fs){
????if(ff.getName().equals(fileName)){
????File localFile=new File(localPath+"/"+ff.getName());
????
????OutputStream is=new FileOutputStream(localFile);
????ftp.retrieveFile(ff.getName()??is);
????is.close();
????}
????}
????
????ftp.logout();
????success=true;
????}catch(IOException e){
????e.printStackTrace();
????}finally{
????if(ftp.isConnected()){
????try{
????ftp.disconnect();
????}catch(IOException ioe){
????}
????}
????}
????return success;
????}<pre></pre>
?????????http://blog.csdn.net/hbcui1984/article/details/2720204