---
title: "ftp 上传下载"
author: "jqs1124"
date: "2012-05-25T15:07:56Z"
tags: []
slug: "ftp"
draft: false
lastmod: "2012-05-25T15:10:53Z"
id: 1541272
url: "https://jqs1124.iteye.com/blog/1541272"
---

这两天要和别的公司数据同步，搞了个ftp下载数据导入自己库里。刚开始找了个ftp工具类，测试的时候是好的（我们公司自己的服务器（2003）），用对接公司ftp地址的时候就出问题了：

1\)

try{  
URL u=new URL([ftp://username:pwd@ip/](ftp://username:pwd@ip/)文件名);  
String \_file=u.getFile();  
System.out.println(\_file+"&gt;&gt;&gt;"+u.getHost());  
InputStream is= u.openStream();  
if(is == null){  
System.out.println("没有能够获取指定的资源");  
return;  
}else{  
BufferedReader in = new BufferedReader(new InputStreamReader(is));  
StringBuffer buffer = new StringBuffer();  
in.readLine(); //读取第一行，接下来从第二行开始循环数据  
String line = "";  
while ((line = in.readLine()) != null) {  
System.out.println(line);  
}  
//System.out.println(buffer.toString());  
}  
}catch(Exception e){  
e.printStackTrace();  
}

报如下错误

java.io.FileNotFoundException:xxxx.txt  
at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnection.java:441)  
at java.net.URL.openStream(URL.java:1009)  
at test.ftp.ftpWriteAndRead.tt(ftpWriteAndRead.java:41)  
at test.ftp.ftpWriteAndRead.main(ftpWriteAndRead.java:30)

可是文件是存在的

 

 

2\)

try {  
FtpClient fc = new FtpClient("xxx.xxx.xxx.xxx");  
fc.login("username", "pwd");  
int ch;  
File fi = new File("d:\\\\MANAGER\_20120523.txt");  
RandomAccessFile getFile = new RandomAccessFile(fi, "rw");  
getFile.seek(0);  
TelnetInputStream fget = fc.get("/MANAGER\_20120523.txt");  
DataInputStream puts = new DataInputStream(fget);  
while ((ch = puts.read()) &gt;= 0) {  
getFile.write(ch);  
}  
fget.close();  
getFile.close();  
fc.closeServer();  
} catch (IOException ex) {

ex.printStackTrace();  
}

报如下错误

java.io.FileNotFoundException: EPRT |1|192.168.xxx.xxx|58029|: 550 'EPRT |1|xxx.xxx.xxx.xxx|38965|': command not understood. Use EPSV command.

at sun.net.ftp.FtpClient.readReply(FtpClient.java:236)  
at sun.net.ftp.FtpClient.issueCommand(FtpClient.java:193)  
at sun.net.ftp.FtpClient.openDataConnection(FtpClient.java:420)  
at sun.net.ftp.FtpClient.get(FtpClient.java:577)  
at test.ftp.ftpDown.main(ftpDown.java:44)

 

xxx:是ip地址

 

一直都这没到解决方案

 

后来在网上有找了一个common-net(说明一下，后来发现上面那种导入的包路径是：sun.net.ftp.FtpClient，后来这种是：org.apache.commons.net.ftp.FTPClient)，所用的jar包不一样

 

针对上面的那位知道解决办法，可以把代码贴出来，也让大家学习一下

 

 

 

可以用的代码见附件

 

针对上面错误的问题有知道的可以发表一下意见，交流一下