前言
最近工作有个数据对接第三方。笔者感觉数据对接的最好方式是把数据封装好推送第三方接口,省时省力。无奈是第三方平台是通过服务器excel文件定时捞取的方式。思考了一下,开码。
场景
如标题.即 JAVA后台实现FTP上传.csv文件。不生成本地文件。
思路
- 1、后台开启定时任务。数据捞取List
- 2、FTP服务器连接
- 3、List 转输入流
- 4、上传至服务器 上代码 ↓。
FTP工具类
/**
* author:FtpUtil
* Date:2020/08/7
* Desc:FTP连接服务器
*/
public class FtpUtil {
private static FtpUtil ftpUtil;
private FTPClient ftpClient;
private String encoding = "GBK";//GBK设值不会中文乱码问题
//构造方法-
private FtpUtil() {
if (ftpClient == null) {
ftpClient = new FTPClient();
}
}
/**
* 获取FTPUtils对象实例-单例
*
* @return FTPUtils对象实例
*/
public synchronized static FtpUtil getInstance() {
if (null == ftpUtil) {
ftpUtil = new FtpUtil();
}
return ftpUtil;
}
/**
* @param serverName 服务地址
* @param username 用户名
* @param password 密码
* @param path 上传到ftpClient服务器哪个路径下
* @return
* @throws Exception
*/
public boolean connectToServer(String serverName, String username, String password, String filePath) throws Exception {
boolean result = false;
int reply;
//连接服务器 默认端口21
ftpClient.connect(serverName, 21);
//登录服务器
ftpClient.login(username, password);
reply = ftpClient.getReplyCode();
//判断返回码是否合法
if (!FTPReply.isPositiveCompletion(reply)) {
//不合法时断开连接
ftpClient.disconnect();
//结束程序
return result;
}
//设置文件操作目录
result = ftpClient.changeWorkingDirectory(filePath);
//设置文件类型-二进制
result = ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//设置缓存区大小
ftpClient.setBufferSize(3072);
ftpClient.setControlEncoding(encoding);
return result;
}
/**
* @param file 上传的文件
* @throws Exception
*/
public boolean upload(File file) throws Exception {
boolean result = false;
try {
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
result = ftpClient.storeFile(file2.getName(), input);
System.out.println("上传结果" + result);
input.close();
} catch (IOException e) {
System.out.println("上传文件失败:" + e.toString());
e.printStackTrace();
} finally {
// 登出服务器并断开连接
logout();
}
return result;
}
/**
* @param fileName 文件名称
* @param is 输入流
* @throws Exception
*/
public boolean storeFile(String serverName, String userName, String password, String filePath, String fileName, InputStream is) throws Exception {
boolean result = false;
try {
//连接服务器
result = connectToServer(serverName, userName, password, filePath);
//判断服务器是否连接成功
if (result) {
//上传文件
result = ftpClient.storeFile(fileName, is);
}
//关闭输入流
is.close();
} catch (IOException e) {
System.out.println("上传文件失败:" + e.toString());
e.printStackTrace();
} finally {
//判断输入流是否存在
if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 登出服务器并断开连接
logout();
}
return result;
}
/**
* 登出服务器并断开连接
*/
public boolean logout() {
boolean result = false;
if (null != ftpClient) {
try {
// 登出服务器
result = ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 判断连接是否存在
if (ftpClient.isConnected()) {
try {
// 断开连接
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return result;
}
/**
* 导出输入流
*/
public ByteArrayInputStream exportCsvStream(List<String> dataList) {
byte[] datas = null;
String str = "";
ByteArrayInputStream inputStream = null;
try {
if (dataList != null && !dataList.isEmpty()) {
for (String data : dataList) {
str += data + "\r";
}
}
datas = str.getBytes(encoding);
inputStream = new ByteArrayInputStream(datas);
} catch (Exception e) {
e.printStackTrace();
}
return inputStream;
}
/**
* 测试方法-主入口
*/
public static void main(String[] args) throws Exception {
String time = DateUtil.format(new Date());
//1、数据
List<String> dataList = new ArrayList<String>();
String headStr =
"周五了(excel第一列),"
+ "明天可以来个床了(excel第二列)"
+ "开心到飞起。(excel第三列),"
+ "(excel第二列)";
dataList.add(dtlStr);//模范数据-
//2、连接Ftp
try {
FtpUtil ftpUtil = FtpUtil.getInstance();
String fileName = "test-" + time + ".csv";
//3、生成文件流
ByteArrayInputStream dataInputStream = ftpUtil.exportCsvStream(dataList);
//4、上传
boolean result=ftpUtil.storeFile("serverName", "userName", "password","filePath",fileName,dataInputStream);
if(!result){
System.out.println("["+fileName+"推送【失败】]");
}
System.out.println(fileName + ",推送【成功】");
} catch (Exception e) {
System.out.println("[" + fileName + ",推送【服务器失败】]" + e.toString());
}
}
}