六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 48|回复: 0

Ftp上传文件调用java代码

[复制链接]

升级  65.33%

40

主题

40

主题

40

主题

秀才

Rank: 2

积分
148
 楼主| 发表于 2013-2-3 14:40:45 | 显示全部楼层 |阅读模式
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上传单个文件测试
     */
    public  void  getAllFileByUser() {
        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上传单个文件测试
     */
    public  void 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);
            }
        }
    }
private  void initFtp(FTPClient ftpClient) throws SocketException,
IOException {
ftpClient.connect(ip);
ftpClient.login(username,password);
}

    /**
     * FTP下载单个文件测试
     */
    public  void 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);
            }
        }
    }
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表