首页 Java java教程 ftp上传实例代码

ftp上传实例代码

Jun 20, 2017 am 09:51 AM
上传

/**
* FTP上传工具类
*
*/
public class FtpUtil {
static Logger logger = Logger.getLogger(FtpUtil.class.getName());
   private String ip;
   private String username;
   private String password;
   private int port = -1;
   private String path;
   private OutputStream os = null;
   private FileInputStream is = null;
private FTPClient ftpClient;
public FTPClient getftpClient() { return ftpClient; }
public void setftpClient(FTPClient ftpClient) {
this.ftpClient = ftpClient;
}
public FtpUtil(){

}
public FtpUtil(String serverIP, String username, String password) {
 this.ip = serverIP;
 this.username = username;
 this.password = password;
}

public FtpUtil(String serverIP, int port, String username, String password) {
 this.ip = serverIP;
 this.username = username;
 this.password = password;
 this.port = port;
 this.ftpClient= new FTPClient();
}
/**
 *
 * @return 是否连接服务器
 */
public boolean ftpLogin() {  
boolean isLogin = false;  
FTPClientConfig ftpClientConfig = new FTPClientConfig();  
ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());  
this.ftpClient.setControlEncoding("GBK");  
this.ftpClient.configure(ftpClientConfig);  
try {    
if (this.port > 0) {    
this.ftpClient.connect(this.ip, this.port);    
}else {    
this.ftpClient.connect(this.ip);    
}  
int reply = this.ftpClient.getReplyCode();    
if (!FTPReply.isPositiveCompletion(reply)) {    
this.ftpClient.disconnect();
logger.info("登录FTP服务失败!");  
return isLogin;    
}    
if(this.ftpClient.login(this.username, this.password)){
this.ftpClient.enterLocalPassiveMode(); // 设置传输协议        
this.ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
logger.info(this.username + "成功登陆FTP服务器");  
isLogin = true;
}else{
logger.info(this.username + "登录FTP服务失败!");
}
}catch (Exception e) {    
e.printStackTrace();  
logger.error( e.getMessage());  
}  
this.ftpClient.setBufferSize(1024 * 2);  
this.ftpClient.setDataTimeout(30 * 1000);  
return isLogin;  
}
/**  
* @退出关闭服务器链接    
*/  
public void ftpLogOut() {  
if (null != this.ftpClient && this.ftpClient.isConnected()) {    
try {    
boolean reuslt = this.ftpClient.logout();// 退出FTP服务器      
if (reuslt) {  
logger.info("成功退出服务器");  
}    
}catch (IOException e) {    
e.printStackTrace();  
logger.warn("退出FTP服务器异常!" + e.getMessage());    
}finally {    
try {      
this.ftpClient.disconnect();
}catch (IOException e) {      
e.printStackTrace();
logger.error("关闭FTP服务器的连接异常!");    
}    
}  
}  
}
/**  
* 上传Ftp文件    
* @param localFile           当地文件  
* @param romotUpLoadePath    上传服务器路径    
*/  
public boolean uploadFile(File localFile, String romotUpLoadePath) {  
BufferedInputStream inStream = null;  
boolean success = false;  
try {    
this.ftpClient.changeWorkingDirectory(romotUpLoadePath);
inStream = new BufferedInputStream(new FileInputStream(localFile));  
success = this.ftpClient.storeFile(localFile.getName(), inStream);    
if (success == true) {  
System.out.println(localFile.getName() + "上传成功");///
return success;    
}  
}catch (FileNotFoundException e) {    
e.printStackTrace();  
}catch (IOException e) {    
e.printStackTrace();  
}finally {    
if (inStream != null) {    
try {      
inStream.close();    
}catch (IOException e) {      
e.printStackTrace();    
}    
}  
}  
return success;  
}  

/**上传文件夹    
* @param localDirectory       本地文件夹      
* @param remoteDirectoryPath  远程路径
*/  
public boolean uploadDirectory(String localDirectory,String remoteDirectoryPath) {  
File src = new File(localDirectory);  
System.out.println(src.getName());
try {    
remoteDirectoryPath = remoteDirectoryPath + src.getName() + "/";    
boolean makeDirFlag = ftpClient.makeDirectory(remoteDirectoryPath);    
System.out.println("localDirectory : " + localDirectory);    
System.out.println("remoteDirectoryPath : " + remoteDirectoryPath);    
System.out.println("src.getName() : " + src.getName());    
System.out.println("remoteDirectoryPath : " + remoteDirectoryPath);    
System.out.println("makeDirFlag : " + makeDirFlag);  
// ftpClient.listDirectories();  
}catch (IOException e) {    
e.printStackTrace();
}  
File[] allFile = src.listFiles();
if(allFile.length==0||allFile.length>0){
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {    
if (!allFile[currentFile].isDirectory()) {    
String srcName = allFile[currentFile].getPath().toString();    
uploadFile(new File(srcName), remoteDirectoryPath);    
}
}  
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {    
if (allFile[currentFile].isDirectory()) {
uploadDirectory(allFile[currentFile].getPath().toString(),  //递归      
remoteDirectoryPath);    
}  
}  
}
/*for (int currentFile = 0;currentFile < allFile.length;currentFile++) {    
if (!allFile[currentFile].isDirectory()) {    
String srcName = allFile[currentFile].getPath().toString();    
uploadFile(new File(srcName), remoteDirectoryPath);    
}
}  
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {    
if (allFile[currentFile].isDirectory()) {
uploadDirectory(allFile[currentFile].getPath().toString(),  //递归      
remoteDirectoryPath);    
}  
}   */
return true;  
}  

以上是ftp上传实例代码的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何在FastAPI中实现文件上传和处理 如何在FastAPI中实现文件上传和处理 Jul 28, 2023 pm 03:01 PM

如何在FastAPI中实现文件上传和处理FastAPI是一个现代化的高性能Web框架,简单易用且功能强大,它提供了原生支持文件上传和处理的功能。在本文中,我们将学习如何在FastAPI框架中实现文件上传和处理的功能,并提供代码示例来说明具体的实现步骤。首先,我们需要导入需要的库和模块:fromfastapiimportFastAPI,UploadF

电脑怎么拍照上传 电脑怎么拍照上传 Jan 16, 2024 am 10:45 AM

电脑只要安装了摄像头就可以进行拍照,但是有些用户还不知道该怎么拍照上传,现在就给大家具体介绍一下电脑拍照的方法,这样用户得到图片之后想上传到哪里都可以了。电脑怎么拍照上传一、Mac电脑1、打开访达,再点击左边的应用程序。2、打开后点击相机应用。3、点击下方的拍照按钮就可以了。二、Windows电脑1、打开下方搜索框,输入相机。2、接着打开搜索到的应用。3、再点击旁边的拍照按钮就可以了。

酷狗上传自己的音乐的简单步骤 酷狗上传自己的音乐的简单步骤 Mar 25, 2024 pm 10:56 PM

1、打开酷狗音乐,点击个人头像。2、点击右上角设置的图标。3、点击【上传音乐作品】。4、点击【上传作品】。5、选择歌曲,然后点击【下一步】。6、最后点击【上传】即可。

如何在QQ音乐上传歌词 如何在QQ音乐上传歌词 Feb 23, 2024 pm 11:45 PM

随着数字化时代的到来,音乐平台成为人们获取音乐的主要途径之一。然而,有时候我们在听歌的时候,发现没有歌词是一件十分困扰的事情。很多人都希望在听歌的时候能够显示歌词,以便更好地理解歌曲的内容和情感。而QQ音乐作为国内最大的音乐平台之一,也为用户提供了上传歌词的功能,使得用户可以更好地享受音乐的同时,感受到歌曲的内涵。下面将介绍一下在QQ音乐上如何上传歌词。首先

Win10电脑上传速度慢怎么解决 Win10电脑上传速度慢怎么解决 Jul 01, 2023 am 11:25 AM

  Win10电脑上传速度慢怎么解决?我们在使用电脑的时候可能会觉得自己电脑上传文件的速度非常的慢,那么这是什么情况呢?其实这是因为电脑默认的上传速度为20%,所以才导致上传速度非常慢,很多小伙伴不知道怎么详细操作,小编下面整理了win11格式化c盘操作步骤,如果你感兴趣的话,跟着小编一起往下看看吧!  Win10上传速度慢的解决方法  1、按下win+R调出运行,输入gpedit.msc,回车。  2、选择管理模板,点击网络--Qos数据包计划程序,双击限制可保留带宽。  3、选择已启用,将带

如何提升电脑上传速度 如何提升电脑上传速度 Jan 15, 2024 pm 06:51 PM

上传速度变得非常慢?相信这是很多朋友用电脑上传东西时候都会遇到的一个问题,在使用电脑传送文件的时候如果遇到网络不稳定,上传的速度就会很慢,那么应该怎么提高网络上传速度呢?下面,小编将电脑上传速度慢的处理方法告诉大家。说到网络速度,我们都知道打开网页的速度,下载速度,其实还有一个上传速度也非常关键,特别是一些用户经常需要上传文件到网盘的,那么上传速度快无疑会给你省下不少时间,那么上传速度慢怎么办?下面,小编给大伙带来了电脑上传速度慢的处理图文。电脑上传速度慢怎么解决点击“开始--运行”或者“窗口键

如何通过PHP快手API接口,实现视频的播放和上传功能 如何通过PHP快手API接口,实现视频的播放和上传功能 Jul 21, 2023 pm 04:37 PM

如何通过PHP快手API接口,实现视频的播放和上传功能导语:随着社交媒体的兴起,大众对于视频内容的需求也逐渐增加。快手作为一款以短视频为主题的社交应用,受到了很多用户的喜爱。本文将介绍如何使用PHP编写代码,通过快手API接口实现视频的播放和上传功能。一、获取访问Token在使用快手API接口之前,首先需要获取访问Token。Token是访问API接口的身份

如何在Vue中实现头像上传功能 如何在Vue中实现头像上传功能 Nov 07, 2023 am 08:01 AM

Vue是一款流行的前端框架,可以用于构建交互性强的应用程序。在开发过程中,上传头像是常见的需求之一。因此,在本文中,我们将介绍如何在Vue中实现头像上传功能,并提供具体的代码示例。使用第三方库为了实现头像上传功能,我们可以使用第三方库,比如vue-upload-component。该库提供了一个上传组件,可以方便地集成到Vue应用程序中。下面是一个简单的示例

See all articles