ftp 上传下载

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

1)

try{
URL u=new URL(ftp://username:pwd@ip/文件名);
String _file=u.getFile();
System.out.println(_file+">>>"+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()) >= 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包不一样

 

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

 

 

 

可以用的代码见附件

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值