我在调用FtpClient类的put(filename)函数时,报错???同样的代码为什么有的计算机上可以执行,有的就报异常?高分求救! 端口可能是一个隐患,还有一个是ftpServer可能没有没有连接上,你最好用其他的ftp工具连接一下,保证你所指向的ftp服务器能使用 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没有啊,Ftp端口怎么会被占呢,用不同的机器Ftp到同一台机器上的,ftp客户端不需要端口的吧。 我用Windows提供的ftp客户端可以连接而且发送到服务器器,但用FtpClient类时,可以连接成功,就是随后调用FtpClient的put函数时报了上述的异常 我用类FtpClientEnd将FtpClient封装起来了:import java.io.*;import sun.net.ftp.*;import sun.net.*;public class FtpClientEnd{ FtpClient aFtpClient=null; private String hostName=null; private String userName=null; private String userPass=null; private String remoteFilePath=null; private String localFilePath=null; private int try_time=3; public FtpClientEnd(String hostName,String userName,String userPass,String remoteFilePath,String localFilePath) { this.hostName=hostName; this.userName=userName; this.userPass=userPass; this.remoteFilePath=remoteFilePath; this.localFilePath=localFilePath; } public boolean connectServer() { if(hostName==null || hostName.equals("")) { System.out.println("ftp hostname can't be blank."); return false; } System.out.println("Connecting to host: "+hostName+"..."); try { aFtpClient=new FtpClient(hostName); aFtpClient.login(userName,userPass); aFtpClient.binary(); } catch(FtpLoginException e) { //System.out.println("无权限访问ftp主机."); System.out.println("not privilidged to access ftp host."); return false; } catch(IOException e) { //System.out.println("连接ftp主机失败."); System.out.println("fail to connect to ftp host."); return false; } catch(SecurityException e) { //System.out.println("无权限连接ftp主机."); System.out.println("not privilidged to connect to ftp host."); return false; } //System.out.println("ftp主机连接成功."); System.out.println("successful to connect to ftp host."); return true; } public boolean upLoadFile(String fileName) { boolean connected=connectServer(); int i=try_time; while(i>0) { if(connected==false) i--; else break; } if(i<=0) return false; RandomAccessFile sendFile=null; TelnetOutputStream out=null; DataOutputStream output=null; int byteData; try { sendFile = new RandomAccessFile(localFilePath+fileName,"r"); } catch(IOException e) { //System.out.println("ftp读本地文件错误。"); System.out.println("ftp read local file error."); closeCon(); return false; } try { aFtpClient.cd(remoteFilePath); } catch(IOException e) { //System.out.println("远程路径错误。"); System.out.println("ftp remote path error."); e.printStackTrace(); closeCon(); return false; } try { //System.out.println("正在发送文件:"+fileName+"..."); System.out.println("Sending file: "+fileName+"..."); sendFile.seek(0);System.out.println("a"); out=aFtpClient.put(fileName);System.out.println("b"); output=new DataOutputStream(out);System.out.println("c"); while(sendFile.getFilePointer"cellpadding="0"bgcolor="#000000"align='center'><trvalign="top"><tdheight="1"></td></tr></table><tablewidth="95"border="0"cellspacing="0"cellpadding="0"align='center'><trvalign="top"><tdcolspan="2"bgcolor="#7C96B8"><tablewidth="100"border="0"cellspacing="0"cellpadding="0"><tr><tdwidth="1"bgcolor="#000000"></td><tdwidth=*><tablewidth="100"border="0"cellspacing="0"cellpadding="4"><tr><td><b>--== 我测试的结果:package com.finalize;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author not attributable * @version 1.0 */import java.io.*;import sun.net.ftp.*;import sun.net.*;public class FtpClientEnd{ FtpClient aFtpClient=null; private String hostName=null; private String userName=null; private String userPass=null; private String remoteFilePath=null; private String localFilePath=null; private int try_time=3; public FtpClientEnd(String hostName,String userName,String userPass,String remoteFilePath,String localFilePath) { this.hostName=hostName; this.userName=userName; this.userPass=userPass; this.remoteFilePath=remoteFilePath; this.localFilePath=localFilePath; } public boolean connectServer() { if(hostName==null || hostName.equals("")) { System.out.println("ftp hostname can't be blank."); return false; } System.out.println("Connecting to host: "+hostName+"..."); try { aFtpClient=new FtpClient(hostName); aFtpClient.login(userName,userPass); aFtpClient.binary(); aFtpClient.cd("1"); } catch(FtpLoginException e) { //System.out.println("无权限访问ftp主机."); System.out.println("not privilidged to access ftp host."); return false; } catch(IOException e) { //System.out.println("连接ftp主机失败."); System.out.println("fail to connect to ftp host."); return false; } catch(SecurityException e) { //System.out.println("无权限连接ftp主机."); System.out.println("not privilidged to connect to ftp host."); return false; } //System.out.println("ftp主机连接成功."); System.out.println("successful to connect to ftp host."); return true; } public boolean upLoadFile(String fileName) throws Exception { boolean connected = connectServer(); int i = try_time; while (i > 0) { if (connected == false) i--; else break; } if (i <= 0) return false; RandomAccessFile sendFile = null; TelnetOutputStream out = null; DataOutputStream output = null; int byteData; try { sendFile = new RandomAccessFile(localFilePath + fileName, "r"); } catch (IOException e) { //System.out.println("ftp读本地文件错误。"); System.out.println("ftp read local file error."); aFtpClient.closeServer();// closeCon(); return false; } try { aFtpClient.cd(remoteFilePath); } catch (IOException e) { //System.out.println("远程路径错误。"); System.out.println("ftp remote path error."); e.printStackTrace(); aFtpClient.closeServer(); return false; } try { //System.out.println("正在发送文件:"+fileName+"..."); System.out.println("Sending file: " + fileName + "..."); sendFile.seek(0); System.out.println("a"); out = aFtpClient.put(fileName); File file_in=new File(localFilePath+fileName); FileInputStream is=new FileInputStream(file_in); byte[] bytes=new byte[1024]; int c; while ((c=is.read(bytes))!=-1){ out.write(bytes,0,c);} System.out.println("b"); //output = new DataOutputStream(out); System.out.println("c"); is.close(); out.close(); } catch(Exception e){ e.printStackTrace();} return true; } public static void main(String[] args) throws Exception { FtpClientEnd ftpclient = new FtpClientEnd("192.168.20.247","administrator","password","1","c:\\"); ftpclient.upLoadFile("CAOut.cer"); } }我的测试结果是Ok的,你看看 我是上传了从c:\CAOut.cer 到服务器目录 1/1/下面 我的代码有的计算机上能正确执行,有的则不行我改用Socket客户端和服务器端了,能实现功能就行了,没办法,活太急 有点好玩的中文编程。问:如何将字符(如中文名)赋给姓名? 关于设定包的问题 有关frame显示的问题! 简单问题 运行javac Test.java的时候抱错 详细情况如下 高难度的数组变换的问题 高分请求源程序!! 关于BDK的问题,帮帮忙,谢谢!!!!!!! 迷惑?!! 请问如何改变contentpane的大小 JAVA的问题 不知如何解决的错误提示 请问各位,哪里能找到ftpClient.class的源代码,究竟放在什么地方???
import java.io.*;
import sun.net.ftp.*;
import sun.net.*;public class FtpClientEnd
{
FtpClient aFtpClient=null;
private String hostName=null;
private String userName=null;
private String userPass=null;
private String remoteFilePath=null;
private String localFilePath=null;
private int try_time=3;
public FtpClientEnd(String hostName,String userName,String userPass,String remoteFilePath,String localFilePath)
{
this.hostName=hostName;
this.userName=userName;
this.userPass=userPass;
this.remoteFilePath=remoteFilePath;
this.localFilePath=localFilePath;
}
public boolean connectServer()
{
if(hostName==null || hostName.equals(""))
{
System.out.println("ftp hostname can't be blank.");
return false;
}
System.out.println("Connecting to host: "+hostName+"...");
try
{
aFtpClient=new FtpClient(hostName);
aFtpClient.login(userName,userPass);
aFtpClient.binary();
}
catch(FtpLoginException e)
{
//System.out.println("无权限访问ftp主机.");
System.out.println("not privilidged to access ftp host.");
return false;
}
catch(IOException e)
{
//System.out.println("连接ftp主机失败.");
System.out.println("fail to connect to ftp host.");
return false;
}
catch(SecurityException e)
{
//System.out.println("无权限连接ftp主机.");
System.out.println("not privilidged to connect to ftp host.");
return false;
}
//System.out.println("ftp主机连接成功.");
System.out.println("successful to connect to ftp host.");
return true;
} public boolean upLoadFile(String fileName)
{
boolean connected=connectServer();
int i=try_time;
while(i>0)
{
if(connected==false)
i--;
else
break;
}
if(i<=0)
return false; RandomAccessFile sendFile=null;
TelnetOutputStream out=null;
DataOutputStream output=null;
int byteData;
try
{
sendFile = new RandomAccessFile(localFilePath+fileName,"r");
}
catch(IOException e)
{
//System.out.println("ftp读本地文件错误。");
System.out.println("ftp read local file error.");
closeCon();
return false;
}
try
{
aFtpClient.cd(remoteFilePath);
}
catch(IOException e)
{
//System.out.println("远程路径错误。");
System.out.println("ftp remote path error.");
e.printStackTrace();
closeCon();
return false;
}
try
{
//System.out.println("正在发送文件:"+fileName+"...");
System.out.println("Sending file: "+fileName+"...");
sendFile.seek(0);
System.out.println("a");
out=aFtpClient.put(fileName);
System.out.println("b");
output=new DataOutputStream(out);
System.out.println("c");
while(sendFile.getFilePointer"cellpadding="0"bgcolor="#000000"align='center'><trvalign="top"><tdheight="1"></td></tr></table><tablewidth="95"border="0"cellspacing="0"cellpadding="0"align='center'><trvalign="top"><tdcolspan="2"bgcolor="#7C96B8"><tablewidth="100"border="0"cellspacing="0"cellpadding="0"><tr><tdwidth="1"bgcolor="#000000"></td><tdwidth=*><tablewidth="100"border="0"cellspacing="0"cellpadding="4"><tr><td><b>--==
package com.finalize;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/import java.io.*;
import sun.net.ftp.*;
import sun.net.*;public class FtpClientEnd
{
FtpClient aFtpClient=null;
private String hostName=null;
private String userName=null;
private String userPass=null;
private String remoteFilePath=null;
private String localFilePath=null; private int try_time=3; public FtpClientEnd(String hostName,String userName,String userPass,String remoteFilePath,String localFilePath)
{
this.hostName=hostName;
this.userName=userName;
this.userPass=userPass;
this.remoteFilePath=remoteFilePath;
this.localFilePath=localFilePath;
} public boolean connectServer()
{
if(hostName==null || hostName.equals(""))
{
System.out.println("ftp hostname can't be blank.");
return false;
}
System.out.println("Connecting to host: "+hostName+"...");
try
{
aFtpClient=new FtpClient(hostName);
aFtpClient.login(userName,userPass);
aFtpClient.binary();
aFtpClient.cd("1");
}
catch(FtpLoginException e)
{
//System.out.println("无权限访问ftp主机.");
System.out.println("not privilidged to access ftp host.");
return false;
}
catch(IOException e)
{
//System.out.println("连接ftp主机失败.");
System.out.println("fail to connect to ftp host.");
return false;
}
catch(SecurityException e)
{
//System.out.println("无权限连接ftp主机.");
System.out.println("not privilidged to connect to ftp host.");
return false;
}
//System.out.println("ftp主机连接成功.");
System.out.println("successful to connect to ftp host.");
return true;
} public boolean upLoadFile(String fileName) throws Exception
{
boolean connected = connectServer();
int i = try_time;
while (i > 0) {
if (connected == false)
i--;
else
break;
}
if (i <= 0)
return false; RandomAccessFile sendFile = null;
TelnetOutputStream out = null;
DataOutputStream output = null;
int byteData; try {
sendFile = new RandomAccessFile(localFilePath + fileName, "r");
}
catch (IOException e) {
//System.out.println("ftp读本地文件错误。");
System.out.println("ftp read local file error.");
aFtpClient.closeServer();// closeCon();
return false;
}
try {
aFtpClient.cd(remoteFilePath);
}
catch (IOException e) {
//System.out.println("远程路径错误。");
System.out.println("ftp remote path error.");
e.printStackTrace();
aFtpClient.closeServer();
return false;
}
try {
//System.out.println("正在发送文件:"+fileName+"...");
System.out.println("Sending file: " + fileName + "...");
sendFile.seek(0);
System.out.println("a");
out = aFtpClient.put(fileName);
File file_in=new File(localFilePath+fileName);
FileInputStream is=new FileInputStream(file_in);
byte[] bytes=new byte[1024];
int c;
while ((c=is.read(bytes))!=-1){
out.write(bytes,0,c);} System.out.println("b");
//output = new DataOutputStream(out);
System.out.println("c");
is.close();
out.close();
}
catch(Exception e){ e.printStackTrace();}
return true;
}
public static void main(String[] args) throws Exception
{
FtpClientEnd ftpclient = new FtpClientEnd("192.168.20.247","administrator","password","1","c:\\");
ftpclient.upLoadFile("CAOut.cer");
} }
我的测试结果是Ok的,你看看
我改用Socket客户端和服务器端了,能实现功能就行了,没办法,活太急