Java Ftp 操作上传与下载

本文提供了一个使用Java实现FTP文件上传和下载的示例,包括两种方式:Apache Commons Net库和Sun的标准库。示例代码展示了如何连接到FTP服务器、上传文件、下载文件以及断开连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.commons.io.IOUtils; import org.apache.commons.net.ftp.FTPClient; import sun.net.TelnetInputStream; import sun.net.TelnetOutputStream; import sun.net.ftp.FtpClient; public class Test { private String ip = "192.168.5.200"; private int poot = 21; private String userName = "weizheng"; private String password = "123456"; private String oldFilePath = "C://Users//Administrator//Desktop//aaa.doc"; private String newFileName = "123456.doc"; private String dowloadPath = "F:/"+newFileName; private void apacheUpload() throws Exception{ FTPClient client = new FTPClient(); FileInputStream fis = null; client.connect(ip); client.login(userName, password); File srcFile = new File(oldFilePath); fis = new FileInputStream(srcFile); //设置上传目录 client.changeWorkingDirectory("/"); client.setBufferSize(1024); client.setControlEncoding("GBK"); //设置文件类型(二进制) client.setFileType(FTPClient.BINARY_FILE_TYPE); client.storeFile("abc.doc", fis); IOUtils.closeQuietly(fis); try { client.disconnect(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭FTP连接发生异常!", e); } } private void apacheDowload() throws Exception{ FTPClient ftpClient = new FTPClient(); FileOutputStream fos = null; try { ftpClient.connect(ip); ftpClient.login(userName, password); fos = new FileOutputStream(dowloadPath); ftpClient.setBufferSize(1024); //设置文件类型(二进制) ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.retrieveFile(oldFilePath, fos); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("FTP客户端出错!", e); } finally { IOUtils.closeQuietly(fos); try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭FTP连接发生异常!", e); } } } private void sunUpload() throws Exception{ FtpClient sunClient = new FtpClient(); TelnetOutputStream os = null; FileInputStream is = null; sunClient.openServer(ip); sunClient.login(userName, password); os = sunClient.put(newFileName); File file = new File(oldFilePath); is = new FileInputStream(file); byte[] b = new byte[1024]; int len = 0; while((len=is.read(b))!=-1){ os.write(b,0,len); } is.close(); os.close(); } private void sunDownload() throws Exception{ FtpClient sunClient = new FtpClient(); TelnetInputStream is = null; FileOutputStream os = null; sunClient.openServer(ip); sunClient.login(userName, password); is = sunClient.get(newFileName); File outputfile = new File(dowloadPath); os = new FileOutputStream(outputfile); byte[] b = new byte[1024]; int len = 0; while((len=is.read(b))!=-1){ os.write(b, 0, len); } is.close(); os.close(); } public static void main(String[] args) throws Exception { Test t = new Test(); t.sunUpload(); t.sunDownload(); // t.apacheUpload(); // t.apacheDowload(); } }


其中apache方式操作需要添加 jar

commons-io.jar

commons-net..jar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值