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;
}
}
【开发问题】ftp2Ftp使用字符集进行传输
于 2023-03-08 15:39:18 首次发布