Java实现文件上传到服务器(FTP方式)详解编程语言

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; 
}

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16823.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论