java上传到ftp_使用Java上传到FTP

从此链接:

Upload files to FTP server using URLConnection class.无需外部库.

String ftpUrl = "ftp://%s:%s@%s/%s;type=i";

String host = "www.myserver.com";

String user = "tom";

String pass = "secret";

String filePath = "E:/Work/Project.zip";

String uploadPath = "/MyProjects/archive/Project.zip";

ftpUrl = String.format(ftpUrl, user, pass, host, uploadPath);

System.out.println("Upload URL: " + ftpUrl);

try {

URL url = new URL(ftpUrl);

URLConnection conn = url.openConnection();

OutputStream outputStream = conn.getOutputStream();

FileInputStream inputStream = new FileInputStream(filePath);

byte[] buffer = new byte[BUFFER_SIZE];

int bytesRead = -1;

while ((bytesRead = inputStream.read(buffer)) != -1) {

outputStream.write(buffer, 0, bytesRead);

}

inputStream.close();

outputStream.close();

System.out.println("File uploaded");

} catch (IOException ex) {

ex.printStackTrace();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值