请问如何用java调用FTP程序,并截获标准输入输出流处理

解决方案 »

  1.   

    看看有没有帮助
    private void ftpUpload(String localfile,String remotefile,String path) throws Exception  
     { 
     //连接ftp服务器
         sun.net.ftp.FtpClient ftpClient = new sun.net.ftp.FtpClient(); 
         ftpClient.openServer("192.168.2.10"); 
         ftpClient.login("root", "root"); 
         //path是ftp服务下主目录的子目录 
       //  if (path.length() != 0)  ftpClient.cd(path); 
         //用2进制上传、下载 
         ftpClient.cd(path);
         ftpClient.binary();       
       
        //上传文件
     TelnetOutputStream os = null; 
         FileInputStream is = null; 
         try {          
             java.io.File file_in = new java.io.File(localfile); 
           //  if (!file_in.exists()) return -1; 
           //  if (file_in.length()==0) return -2; 
             os = ftpClient.put(remotefile); 
           //  result = file_in.length(); 
             is = new FileInputStream(file_in); 
             byte[] bytes = new byte[1024]; 
             int c; 
             while ((c = is.read(bytes)) != -1) { 
                  os.write(bytes, 0, c); 
             } 
         } finally { 
             if (is != null) { 
                 is.close(); 
             } 
             if (os != null) { 
                os.close(); 
             } 
         } 
       //传送完毕 关闭ftp连接
         try  
       { 
          if (ftpClient != null)  
          { 
            ftpClient.closeServer();      
          } 
       } catch (IOException e) { 
          e.printStackTrace(); 
       } 
     } 
      

  2.   

    半抄  半写的   有用就拿走
    package com.excellence.pigeonhole.util;import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;import org.apache.commons.net.ProtocolCommandEvent;
    import org.apache.commons.net.ProtocolCommandListener;
    import org.apache.commons.net.ftp.FTP;
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPReply;import com.excellence.pigeonhole.general.Information;public class FtpUtil {
    private static FtpUtil ftpu = null;
    private FTPClient ftp = null;
    private String userName = Information.FTP_USERNAME_STRING;
    private String passWord = Information.FTP_PASSWORD_STRING;
    private int port = Integer.parseInt(Information.FTP_PORT_STRING);
    private String ip = Information.FTP_IP_STRING;
    private String root = Information.FTP_ROOT_STRING; private FtpUtil() {
    initFtp();
    } public void initFtp() {
    ftp = new FTPClient();
    ftp.addProtocolCommandListener(new ProtocolCommandListener() {
    String message = ""; public void protocolCommandSent(ProtocolCommandEvent event) {
    message = event.getMessage();
    if ("pass".equalsIgnoreCase(event.getCommand())) {
    // 隐藏密码
    message = message.substring(0, message.indexOf(" "))
    + " ******";
    }
    System.out.println(message);
    } public void protocolReplyReceived(ProtocolCommandEvent event) {
    System.out.println(event.getMessage());
    }
    });
    try {
    int reply;
    ftp.connect(ip, port); reply = ftp.getReplyCode();
    System.out.println("reply code: " + reply);
    String msg;
    if (!FTPReply.isPositiveCompletion(reply)) {
    ftp.disconnect();
    msg = "FTP服务器拒绝访问";
    System.out.println(msg);
    }
    if (!ftp.login(userName, passWord)) {
    ftp.logout();
    msg = "用户名或者密码错误";
    System.out.println(msg);
    }
    String[] folders = root.split("/");
    for (int i = 0; i < folders.length; i++) {
    String folder = folders[i];
    if (folder.trim().length() == 0)
    continue; if (!ftp.changeWorkingDirectory(folder)) {
    if (ftp.makeDirectory(folder)) {
    if (!ftp.changeWorkingDirectory(folder)) {
    msg = "不能转到" + folder + "目录";
    System.out.println(msg);
    }
    } else {
    msg = "不能在ftp服务器上建立" + folder + "目录";
    System.out.println(msg);
    }
    }
    }
    System.out.println("Current working directory: "
    + ftp.printWorkingDirectory());
    } catch (IOException e) {
    e.printStackTrace();
    System.out.println("connectFtp发生异常" + e.getMessage());
    disconnectFtp();
    }
    } public static FtpUtil getInstance() {
    if (ftpu == null) {
    ftpu = new FtpUtil();
    }
    return ftpu;
    } public void upLoadFile(String realPath, String fileName) {
    if (ftp == null) {
    initFtp();
    }
    File file = new File(realPath);
    if (!file.exists()) {
    System.out.println("路径无效:" + realPath);
    return;
    }
    if (!file.isFile()) {
    System.out.println("路径指向的不是一个有效的文件:" + realPath);
    return;
    }
    String filename = fileName;
    try {
    filename = new String(fileName.getBytes(), "iso8859_1");
    } catch (UnsupportedEncodingException e) {
    filename = fileName;
    }
    InputStream is = null;
    try {
    is = new FileInputStream(file);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.setControlEncoding("GBK");
    ftp.storeFile(filename, is);
    // disconnectFtp();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (is != null) {
    try {
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    } public void upLoadFile(InputStream is, String fileName) {
    if (ftp == null) {
    initFtp();
    }
    String filename = fileName;
    try {
    filename = new String(fileName.getBytes(), "iso8859_1");
    } catch (UnsupportedEncodingException e) {
    filename = fileName;
    }
    try {
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.setControlEncoding("GBK");
    ftp.storeFile(filename, is);
    // disconnectFtp();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (is != null) {
    try {
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    } public void disconnectFtp() {
    if (ftp != null && ftp.isConnected()) {
    try {
    ftp.disconnect();
    ftp = null;
    } catch (IOException e) {
    }
    }
    }
    }