Java实现文件上传到服务器(FTP方式)

1,jar包:commons-net-3.3.jar

2,实现代码:

    //FTP传输到数据库服务器
private boolean uploadServerByFtps(){
boolean flag = true;
//获取客户端数据文件路径
String client_path = Pub.getPropertiesValue("analysis", "analysis.client.data.path");
//获取数据库服务器上的存放数据文件路径
String server_path = Pub.getPropertiesValue("analysis", "analysis.server.data.path"); String hostname = Pub.getPropertiesValue("analysis", "ftp.hostname");
String ftpusername = Pub.getPropertiesValue("analysis", "ftp.username");
String ftppwd = Pub.getPropertiesValue("analysis", "ftp.pwd");
int port = 21;
logger.info("ftp server infor:hostname:"+hostname+" ftpusername:"+ftpusername+" ftppwd:"+ftppwd+" port:"+port); //遍历客户端数据文件路径下的txt文件,然后采用FTP上传
File client_dir = new File(client_path);
if(!client_dir.exists()){
logger.info("The file directory on the host you want to read does not exist");
return flag;
}
File client_files[] = client_dir.listFiles();
if(client_files.length == 0){
logger.info("You want to read the file directory without any files");
return flag;
}
//开始遍历文件
//创建ftp客户端
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("GBK");
for(int i=0;i<client_files.length;i++){
String getfileName = client_files[i].getName();
String getfileNamePath = client_files[i].getPath();
//只对txt文件做处理
if((getfileName.substring(getfileName.lastIndexOf(".")).trim().equals(".txt"))){
//todo:增加控制文件(已经传输过的文件)的传输 try {
//链接ftp服务器
ftpClient.connect(hostname, port);
//登录ftp
ftpClient.login(ftpusername, ftppwd);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
logger.info("Returns a 530 password username error or the current user does not have permission to close the FTP connection");
return false;
}
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// ftpClient.makeDirectory("path");//在root目录下创建文件夹
String server_file_name = server_path+ getfileName;
logger.info("server_file_name>>>>>>: " + server_file_name);
logger.info("getfileNamePath>>>>>>: " + getfileNamePath); //读取源文件流(客户端文件)
InputStream client_fileInput = new FileInputStream(getfileNamePath);
//传送到服务端
ftpClient.storeFile(server_file_name, client_fileInput);//文件你若是不指定就会上传到root目录下
client_fileInput.close();
ftpClient.logout();
logger.info("ftp is success");
} catch (SocketException e) {
// TODO Auto-generated catch block
logger.info("port problem: "+e.toString());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
logger.info("IO problem: "+e.toString());
e.printStackTrace();
} catch (Exception e) {
logger.info("ftp problem: "+e.toString());
e.printStackTrace();
}finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
logger.info("IO problem: "+ioe.toString());
}
} }
}
} return flag;
}

最新文章

  1. 【MySQL】 查询某个数据库有多少张数据表
  2. IDT HOOK思路整理
  3. SQL将JSON转成列
  4. Luogu题目集合[6/未完待续]
  5. MySQL中的外键是什么、有什么作用
  6. Android调用浏览器打开网址遇到的问题
  7. Android应用开发基础之九:内容提供者(ContentProvider)
  8. 通过Docker配置DNS服务器
  9. HDU-5086-Revenge of Segment Tree
  10. 根据GPS经纬度判断当前所属的市区
  11. Hadoop集群
  12. 【个人笔记】《知了堂》MySQL三种关系:一对一,一对多,多对多。
  13. PHP 环境搭建篇
  14. 81. Search in Rotated Sorted Array II (中等)
  15. ssh远程登录操作 和ssh信任
  16. Web后台快速开发框架
  17. LeetCode(103): 二叉树的锯齿形层次遍历
  18. memcache、redis原理对比
  19. mvn2gradle
  20. 输出链表中倒数第k个节点

热门文章

  1. Android英文文档翻译系列(1)——AlarmManager
  2. MUI Hbuilder设置模拟器运行APP项目
  3. MQTT的学习研究(四)moquette-mqtt 的使用之mqtt Blocking API客户端订阅并接收主题信息
  4. 【Android N 7.1.1】 ActivityManagerService 获取cpu状态
  5. java.lang.NoSuchMethodException:com.yxq.action.AdminAction.addGoods()《转载》
  6. 交换机工作原理、MAC地址表、路由器工作原理详解
  7. Laravel 5.7 No &#39;Access-Control-Allow-Origin&#39; header is present on the request resource
  8. Xshell配置ssh免密码登录-密钥公钥(Public key)
  9. vuls漏洞扫描工具
  10. Asp.NET 与 WebApi 共享Session