Java FTP上传

来源:互联网 发布:多线程共享数据的方式 编辑:程序博客网 时间:2024/06/08 12:53
java 代码
  1. package common.ftpOperate;   
  2.   
  3. import java.io.FileInputStream;   
  4. import java.io.IOException;   
  5. import java.io.InputStream;   
  6. import org.apache.commons.net.ftp.FTP;   
  7. import org.apache.commons.net.ftp.FTPClient;   
  8. import org.apache.commons.net.ftp.FTPConnectionClosedException;   
  9. import org.apache.commons.net.ftp.FTPReply;   
  10. public class FtpUpload {   
  11.     String hostName = "211.137.79.177";   
  12.   
  13.     String userName = "rbtdiy";   
  14.   
  15.     String password = "rbtdiy";   
  16.   
  17.     String remoteDir = "rbtdiy";   
  18.     String port = "rbtdiy";   
  19.   
  20.     public FtpUpload() {   
  21.   
  22.            
  23.   
  24.   
  25.             if (remoteDir == null || remoteDir.equalsIgnoreCase("")) {   
  26.   
  27.                 remoteDir = null;   
  28.             }   
  29.   
  30.            
  31.     }   
  32.   
  33.     public boolean UploadFile(String localfilename, String remotefilename) {   
  34.         FTPClient ftp = new FTPClient();   
  35.         int reply;   
  36.         try {   
  37.             ftp.connect(hostName,Integer.parseInt(port));   
  38.             reply = ftp.getReplyCode();   
  39.             if (!FTPReply.isPositiveCompletion(reply)) {   
  40.                 ftp.disconnect();   
  41.                 return false;   
  42.             }   
  43.         } catch (IOException e) {   
  44.             if (ftp.isConnected()) {   
  45.                 try {   
  46.                     ftp.disconnect();   
  47.                 } catch (IOException f) {   
  48.                     return false;   
  49.                 }   
  50.             }   
  51.         }   
  52.   
  53.         try {   
  54.             if (!ftp.login(userName, password)) {   
  55.                 ftp.logout();   
  56.                 return false;   
  57.             }   
  58.                
  59.             ftp.setFileType(FTP.BINARY_FILE_TYPE);   
  60.             //ftp.setFileType(FTP.ASCII_FILE_TYPE);   
  61.             ftp.enterLocalPassiveMode();   
  62.             //ftp.changeWorkingDirectory(remoteDir);   
  63.   
  64.             ftp.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);   
  65.             InputStream input = new FileInputStream(localfilename);   
  66.             if (input == null) {   
  67.                 System.out.println("本地文件不存在");   
  68.   
  69.             }   
  70.             ftp.storeFile(remotefilename, input);   
  71.             input.close();   
  72.             ftp.logout();   
  73.         } catch (FTPConnectionClosedException e) {   
  74.             if (ftp.isConnected()) {   
  75.                 try {   
  76.                     ftp.disconnect();   
  77.                 } catch (IOException f) {   
  78.                     return false;   
  79.                 }   
  80.             }   
  81.             return false;   
  82.         } catch (IOException e) {   
  83.             if (ftp.isConnected()) {   
  84.                 try {   
  85.                     ftp.disconnect();   
  86.                 } catch (IOException f) {   
  87.                     return false;   
  88.                 }   
  89.             }   
  90.             return false;   
  91.         } finally {   
  92.             if (ftp.isConnected()) {   
  93.                 try {   
  94.                     ftp.disconnect();   
  95.                 } catch (IOException f) {   
  96.                     return false;   
  97.                 }   
  98.             }   
  99.         }   
  100.         return true;   
  101.     }   
  102.   
  103.     public static void main(String args[]) throws Exception {   
  104.         FtpUpload ftpup = new FtpUpload();   
  105.         ftpup.UploadFile("c:/java2html.java""java2html.java");       
  106.         }   
  107.            
  108.     }   
  109. }  
原创粉丝点击