FTP上传文件

Java代码 复制代码 收藏代码
  1. package cn.com.songjy;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileInputStream;   
  5. import java.io.IOException;   
  6.   
  7. import org.apache.log4j.Logger;   
  8.   
  9. import sun.net.TelnetOutputStream;   
  10. import sun.net.ftp.FtpClient;   
  11.   
  12. public class FTPUPload {   
  13.   
  14.     private static Logger log = Logger.getLogger(FTPUPload.class);   
  15.     static FtpClient ftpClient = null;   
  16.   
  17.     /**  
  18.      * 建立FTP连接  
  19.      * @method connection  
  20.      * @param ip 远程计算机IP  
  21.      * @param user ftp用户  
  22.      * @param password ftp用户的密码  
  23.      * @param newpath 远程计算机目录  
  24.      * @return  
  25.      */  
  26.     public static boolean connection(String ip, String user, String password,   
  27.             String newpath) {   
  28.            
  29.         log.info("远程IP地址:"+ip);   
  30.         log.info("FTP账户:"+user);   
  31.         log.info("FTP账户密码:"+password);   
  32.         log.info("远程目录:"+newpath);   
  33.            
  34.         ftpClient = new FtpClient();   
  35.         try {   
  36.             ftpClient.openServer(ip);   
  37.             ftpClient.login(user, password);   
  38.             if (newpath.length() > 0) {   
  39.                 ftpClient.cd(newpath);   
  40.             }   
  41.             ftpClient.binary();   
  42.             return true;   
  43.         } catch (IOException e) {   
  44.             e.printStackTrace();   
  45.         }   
  46.         return false;   
  47.     }   
  48.   
  49.     /**  
  50.      * 上传文件至远程计算机  
  51.      * @method upload  
  52.      * @param ip 远程计算机IP  
  53.      * @param user ftp用户  
  54.      * @param password ftp用户的密码  
  55.      * @param path 需上传文件的路径  
  56.      * @param newPath 远程ftp上传目录  
  57.      */  
  58.     public static void upload(String ip, String user, String password,   
  59.             String path, String newPath) {   
  60.            
  61.         File file = new File(path);   
  62.            
  63.         if (!file.exists()){   
  64.             log.info("文件不存在!");   
  65.             return;   
  66.         }   
  67.            
  68.         log.info("上传文件全路径:"+file.getAbsolutePath());   
  69.            
  70.         //建立FTP连接   
  71.         boolean conn=connection(ip,user,password,newPath);   
  72.            
  73.         if(false==conn){   
  74.             log.info("建立FTP连接失败!");   
  75.             return;   
  76.         }   
  77.            
  78.         System.getProperties().put("file.encoding""UTF-8");   
  79.         TelnetOutputStream os = null;   
  80.         FileInputStream is = null;   
  81.            
  82.         try {   
  83.                
  84.             is = new FileInputStream(file);   
  85.             File newfile=new File(newPath);   
  86.             if(newfile.exists()){   
  87.                 newfile.mkdirs();   
  88.                 newfile.createNewFile();   
  89.             }   
  90.                
  91.                
  92.             os = ftpClient.put(newPath+file.getName());   
  93.             byte[] b = new byte[1024];   
  94.             int c;   
  95.             log.info("开始上传文件...");   
  96.             while (-1 != (c=is.read(b))) {   
  97.                 os.write(b, 0, c);   
  98.             }   
  99.             os.flush();   
  100.             os.close();   
  101.             is.close();   
  102.                
  103.             log.info("文件上传完毕!");   
  104.                
  105.             if (null != ftpClient)   
  106.                 ftpClient.closeServer();   
  107.                
  108.         } catch (IOException e) {   
  109.             System.err.println("上传文件异常!");   
  110.             e.printStackTrace();   
  111.         }   
  112.   
  113.     }   
  114.   
  115.     public static void main(String[] args) {   
  116.         upload("192.168.23.1""songjy""123456","E:\\Workspaces\\java\\srt\\ezg\\demo-web\\pom.xml""/songjy/upload/");   
  117.     }   
  118.   
  119. }  


posted on 2013-02-15 11:30  蜜雪薇琪  阅读(318)  评论(0编辑  收藏  举报