黄中 发表于 2016-6-9 07:18:12

Ftp上传文件调用java代码

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.net.SocketException;
import java.util.List;

public class FTPmain {
    private String ip = "";         
    private String username = "";         
    private String password = "";         
    private int port = -1;      
    public FTPmain(String serverIP, String username, String password) {      
      this.ip = serverIP;      
      this.username = username;      
      this.password = password;      
    }      
         
    public FTPmain(String serverIP, int port, String username, String password) {      
      this.ip = serverIP;      
      this.username = username;      
      this.password = password;      
      this.port = port;      
    }      
    /**
   * FTP上传单个文件测试
   */
    publicvoidgetAllFileByUser() {
      FTPClient ftpClient = new FTPClient();
      FileInputStream fis = null;

      try {
            initFtp(ftpClient);
            String[] list=ftpClient.listNames();
            for(String s:list){
            String ss= new String(s.getBytes("ISO-8859-1"),"GBK");
    System.out.println(ss);
            }

//            File srcFile = new File("E:\\test.txt");
//            fis = new FileInputStream(srcFile);
//            //设置上传目录
//            ftpClient.changeWorkingDirectory("/");
//            ftpClient.setBufferSize(1024);
//            ftpClient.setControlEncoding("GBK");
//            //设置文件类型(二进制)
//            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//            ftpClient.storeFile("2.txt", fis);
      } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
      } finally {
            IOUtils.closeQuietly(fis);
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常!", e);
            }
      }
    }   

    /**
   * FTP上传单个文件测试
   */
    publicvoid ftpUpload() {
      FTPClient ftpClient = new FTPClient();
      FileInputStream fis = null;

      try {
            initFtp(ftpClient);

            File srcFile = new File("E:\\test.txt");
            fis = new FileInputStream(srcFile);
            //设置上传目录
            ftpClient.changeWorkingDirectory("/");
            ftpClient.setBufferSize(1024);
            ftpClient.setControlEncoding("GBK");
            //设置文件类型(二进制)
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.storeFile("2.txt", fis);
      } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
      } finally {
            IOUtils.closeQuietly(fis);
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常!", e);
            }
      }
    }
privatevoid initFtp(FTPClient ftpClient) throws SocketException,
IOException {
ftpClient.connect(ip);
ftpClient.login(username,password);
}

    /**
   * FTP下载单个文件测试
   */
    publicvoid ftpDownload(String uploadUrl,String saveUrl) {
      FTPClient ftpClient = new FTPClient();
      FileOutputStream fos = null;

      try {
            initFtp(ftpClient);

            String remoteFileName = uploadUrl;
            fos = new FileOutputStream(saveUrl);

            ftpClient.setBufferSize(1024);
            //设置文件类型(二进制)
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.retrieveFile(remoteFileName, fos);
      } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
      } finally {
            IOUtils.closeQuietly(fos);
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP连接发生异常!", e);
            }
      }
    }
}
页: [1]
查看完整版本: Ftp上传文件调用java代码