【开发问题】ftp2Ftp使用字符集进行传输

package com.example.demo;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.*;

public class ftp2FtpByByte {

    public static void main(String arg[]) throws Exception {
        //String sourcefilePath = "D:\\ftp\\sourceCsv\\演示文稿1(1).pptx";
        String sourceFilePath = "/sourceCsv/";
        String filename = "source.csv";
        String host = "127.0.0.1";
        int port = 21;
        String username = "wx";
        String password = "123456";
        String targetFilePath = "/target/";
        ftp2Ftp( host, port, username, password, sourceFilePath+"\\"+filename, targetFilePath+"\\"+filename);
    }

    public static void ftp2Ftp(String host, int port, String username, String password, String sourceFilePath, String targetFilePath) throws Exception {
        InputStream inputStream =  getInputStream( host, port, username, password, sourceFilePath);
        /*获取的inputSteam 转化为byte 然后写入*/
        byte[] buffer = IOUtils.toByteArray(inputStream);
        OutputStream outputStream = getOutputStream( host, port, username, password, targetFilePath);
        outputStream.write(buffer);
        outputStream.close();
    }


    public static InputStream getInputStream(String host,int port,String username,String password,String filePath) {
        try {
            FTPClient ftpClient = getConnect(host, port, username, password);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            String filepath2 = new String(filePath.getBytes("UTF-8"), FTP.DEFAULT_CONTROL_ENCODING);
            return ftpClient.retrieveFileStream(filepath2);
        } catch (IOException e) {
            String message = String.format("读取文件 : [%s] 时出错,请确认文件:[%s]存在且配置的用户有权限读取", filePath, filePath);
            return  null;
        }
    }


    public static OutputStream getOutputStream(String host, int port, String username, String password, String filePath) {
        try {
            FTPClient ftpClient = getConnect(host, port, username, password);
            String parentDir = filePath.substring(0,StringUtils.lastIndexOf(filePath, IOUtils.DIR_SEPARATOR));
            ftpClient.changeWorkingDirectory(new String(parentDir.getBytes("GBK"),FTP.DEFAULT_CONTROL_ENCODING));

            String ftpPath2 = new String(filePath.getBytes("GBK"),FTP.DEFAULT_CONTROL_ENCODING);

            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            OutputStream writeOutputStream = ftpClient.appendFileStream(ftpPath2);
            String message = String.format(
                    "打开FTP文件[%s]获取写出流时出错,请确认文件%s有权限创建,有权限写出等", filePath,
                    filePath);
            if (null == writeOutputStream) {
                System.out.println(message);
            }

            return writeOutputStream;
        } catch (IOException e) {
            String message = String.format(
                    "写出文件 : [%s] 时出错,请确认文件:[%s]存在且配置的用户有权限写, errorMessage:%s",
                    filePath, filePath, e.getMessage());
            System.out.println(message);
            return null;
        }
    }


    public static FTPClient getConnect(String host, int port, String username, String password) throws IOException {
        FTPClient ftp = new FTPClient();
        ftp.connect(host, port);// 连接FTP服务器
        int reply;
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
        ftp.login(username, password);// 登录
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
        }
        return ftp;
    }



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值