JAVA中使用FTPClient上传下载
方法一:这种方法网上有许多
可以用下面的方式,但是支持批量上传和连续操作
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
比如,我获取了文件列表就不能够在进行下载文件,在比如我下载了一个文件就不能够下载第二个文件;
方法二:Apache的FTPClient。
http://commons.apache.org/proper/commons-net/
commons-net-1.4.1.zip
支持网络协议如下:
FTP
NNTP
SMTP
POP3
Telnet
TFTP
Finger
Whois
rexec/rcmd/rlogin
Time (rdate) and Daytime
Echo
Discard
NTP/SNTP
import org.apache.commons.io.IOUtils; import org.apache.commons.net.ftp.FTPClient; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.FileOutputStream; /** * Apache commons-net 试用一把,看看FTP客户端工具做的好用不 * * @author : leizhimin,2008-8-20 14:00:38。<p> */ public class FtpTest { public static void main(String[] args) { testUpload(); testDownload(); } /** * FTP上传单个文件测试 */ public static void testUpload() { FTPClient ftpClient = new FTPClient(); FileInputStream fis = null; try { ftpClient.connect("192.168.14.117"); ftpClient.login("admin", "123"); File srcFile = new File("C:\\new.gif"); fis = new FileInputStream(srcFile); //设置上传目录 ftpClient.changeWorkingDirectory("/admin/pic"); ftpClient.setBufferSize(1024); ftpClient.setControlEncoding("GBK"); //设置文件类型(二进制) ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.storeFile("3.gif", fis); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("FTP客户端出错!", e); } finally { IOUtils.closeQuietly(fis); try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭FTP连接发生异常!", e); } } } /** * FTP下载单个文件测试 */ public static void testDownload() { FTPClient ftpClient = new FTPClient(); FileOutputStream fos = null; try { ftpClient.connect("192.168.14.117"); ftpClient.login("admin", "123"); String remoteFileName = "/admin/pic/3.gif"; fos = new FileOutputStream("c:/down.gif"); ftpClient.setBufferSize(1024); //设置文件类型(二进制) ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.retrieveFile(remoteFileName, 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); } } } }
相关推荐
pigsmall 2020-11-19
SXIAOYI 2020-09-16
linuxprobe0 2013-04-01
83560193 2013-06-25
kidneybeans 2013-06-17
加菲猫园 2013-06-16
年轻就要对味 2014-07-11
稻草人的高粱地 2014-07-02
smilebestSun 2014-06-12
xusong 2014-05-17
zfszhangyuan 2013-09-12
80183053 2013-09-12
gaozhlzh 2013-09-11
87453169 2014-01-17
Tom天天 2013-07-12
donghedonghe 2013-05-31
tdeclipse 2011-02-28