java从FTP(FileZilla Server)上传文件

时间: 2024-01-30 09:44:50 浏览: 32
以下是使用Java实现从FTP服务器(例如FileZilla Server)上传文件的代码示例: ```java import java.io.FileInputStream; import java.io.IOException; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; public class FTPUploader { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String user = "username"; String pass = "password"; String localFilePath = "C:/Users/Username/Documents/file.txt"; String remoteFilePath = "/public_html/file.txt"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(user, pass); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); FileInputStream inputStream = new FileInputStream(localFilePath); ftpClient.storeFile(remoteFilePath, inputStream); inputStream.close(); ftpClient.logout(); } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); } finally { try { if (ftpClient.isConnected()) { ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } } } } ``` 在上述代码中,需要修改以下变量: - `server`:FTP服务器地址。 - `port`:FTP服务器端口号,默认为21。 - `user`:登录FTP服务器的用户名。 - `pass`:登录FTP服务器的密码。 - `localFilePath`:要上传的本地文件的路径。 - `remoteFilePath`:要上传到FTP服务器的远程文件路径。 注意,以上代码使用了Apache Commons Net库,需要先将其添加到项目依赖中。

相关推荐

最新推荐

recommend-type

FileZilla Server-1.1.0配置使用.docx

Windows 网络邻居经常罢工,而且win10,win11跟win7, xp经常相互无法识别,一怒之下专用FileZilla Server,一次配置,永久无忧!网上关于FileZilla Server的配置教程旧版居多,此教程是目前最新的FileZilla Server-...
recommend-type

JAVA技术实现上传下载文件到FTP服务器(完整)

主要介绍了JAVA技术实现上传下载文件到FTP服务器(完整),本文使用 Apache Jakarta Commons Net(commons-net-3.3.jar) 基于FileZilla Server服务器实现FTP服务器上文件的上传/下载/删除等操作,需要的朋友可以参考下
recommend-type

Filezilla server配置FTP服务器中的各种问题与解决方法

主要介绍了Filezilla server配置FTP服务器中的各种问题与解决方法,需要的朋友可以参考下
recommend-type

使用FileZilla_Server搭建ftps和ftp服务器

使用FileZilla_Server搭建ftps和ftp服务器使用FileZilla_Server搭建ftps和ftp服务器使用FileZilla_Server搭建ftps和ftp服务器使用FileZilla_Server搭建ftps和ftp服务器
recommend-type

springboot以FTP方式上传文件到远程服务器的流程

本文给大家介绍jwt登录认证及鉴权的流程,对springboot ftp上传文件到远程服务器的相关知识感兴趣的朋友一起看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。