本段程序是实现按照本地机器上的一个文件夹目录结构传送到FTP服务器上的功能,为了简化方便高手的解答我只传送一层,多层跟这个一样的问题!下面程序中一个是判断文件和文件夹的,两个相互分开时,可以运行,但放在一起会出错!
错误信息:
java.io.FileNotFoundException: CWD /2: 550 /2/???¨??????: Failed to create. at sun.net.ftp.FtpClient.readReply(Unknown Source)
at sun.net.ftp.FtpClient.issueCommand(Unknown Source)
at sun.net.ftp.FtpClient.issueCommandCheck(Unknown Source)
at sun.net.ftp.FtpClient.cd(Unknown Source)
at shareit.ftp.FtpConnection.run(FTPtxt.java:110)
sun.net.ftp.FtpProtocolException: PORT :500 'EPRT': command not understood. at sun.net.ftp.FtpClient.openDataConnection(Unknown Source)
at sun.net.ftp.FtpClient.put(Unknown Source)
at shareit.ftp.FtpConnection.run(FTPtxt.java:118)
sun.net.ftp.FtpProtocolException: PORT :500 'EPRT': command not understood. at sun.net.ftp.FtpClient.openDataConnection(Unknown Source)
at sun.net.ftp.FtpClient.put(Unknown Source)
at shareit.ftp.FtpConnection.run(FTPtxt.java:118)
sun.net.ftp.FtpProtocolException: PORT :500 'EPRT': command not understood. at sun.net.ftp.FtpClient.openDataConnection(Unknown Source)
at sun.net.ftp.FtpClient.put(Unknown Source)
at shareit.ftp.FtpConnection.run(FTPtxt.java:118)源程序是:
package shareit.ftp;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;class Node
{
File[] fle;
}
class FtpConnection extends Thread
{ //private String user;
//private String password;
private FtpClient ftpclient;
FtpConnection()
{
try{ftpclient=new FtpClient("192.168.0.66",21);
Login();
}
catch(Exception e){}
}
public void Login()
{
try {
ftpclient.login("winterzd", "2251780");
} catch (IOException e) {
e.printStackTrace();
}
}
public void Logout()
{
ftpclient.sendServer("QUIT\r\n");
int reply = 0;
try {
reply = ftpclient.readServerResponse();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(reply);
}
public void run()
{
try {
//readirt();
//*********************************************
File file=new File("D:\\2");
File[] filelist=file.listFiles();
int count=filelist.length;
System.out.println(count);
int i=count-1;
while(i>=0)
{
if(filelist[i].isDirectory()==false)
/*{try{
String path=filelist[i].getAbsolutePath();
String[] pathsplit=path.split("\\\\");
int j=pathsplit.length-1;
int m=1;
String pathafter="/";
while(m<=j)
{
if(m<j)pathafter=pathafter.concat(pathsplit[m])+"/";
else pathafter=pathafter.concat(pathsplit[m]);
m++;
}
System.out.println(pathafter);
// pathafter="/test";
ftpclient.sendServer("XMKD" + pathafter + "\r\n");
}
catch (Exception e) {e.printStackTrace();}
}
else{*/
try{
String path=filelist[i].getParent();
String[] pathsplit=path.split("\\\\");
int j=pathsplit.length-1;
int m=1;
String pathafter="/";
while(m<=j)
{
if(m<j)pathafter=pathafter.concat(pathsplit[m])+"/";
else pathafter=pathafter.concat(pathsplit[m]);
m++;
}
ftpclient.cd(pathafter);
String name=filelist[i].getName(); //File f=new File(path,name);
ftpclient.binary();
FileInputStream input=new FileInputStream(filelist[i]);
TelnetOutputStream output=ftpclient.put(name);
int bytesRead;
while((bytesRead=input.read())!=-1)
output.write(bytesRead);
input.close();
output.close();
}
catch (Exception e) {e.printStackTrace();}
//}
i--;
}
//**********************************************
}
catch(Exception ee){}
Logout();
}
}public class FTPtxt {
public static void main(String args[])
{
FtpConnection ftp=new FtpConnection();
ftp.start();
}
}
错误信息:
java.io.FileNotFoundException: CWD /2: 550 /2/???¨??????: Failed to create. at sun.net.ftp.FtpClient.readReply(Unknown Source)
at sun.net.ftp.FtpClient.issueCommand(Unknown Source)
at sun.net.ftp.FtpClient.issueCommandCheck(Unknown Source)
at sun.net.ftp.FtpClient.cd(Unknown Source)
at shareit.ftp.FtpConnection.run(FTPtxt.java:110)
sun.net.ftp.FtpProtocolException: PORT :500 'EPRT': command not understood. at sun.net.ftp.FtpClient.openDataConnection(Unknown Source)
at sun.net.ftp.FtpClient.put(Unknown Source)
at shareit.ftp.FtpConnection.run(FTPtxt.java:118)
sun.net.ftp.FtpProtocolException: PORT :500 'EPRT': command not understood. at sun.net.ftp.FtpClient.openDataConnection(Unknown Source)
at sun.net.ftp.FtpClient.put(Unknown Source)
at shareit.ftp.FtpConnection.run(FTPtxt.java:118)
sun.net.ftp.FtpProtocolException: PORT :500 'EPRT': command not understood. at sun.net.ftp.FtpClient.openDataConnection(Unknown Source)
at sun.net.ftp.FtpClient.put(Unknown Source)
at shareit.ftp.FtpConnection.run(FTPtxt.java:118)源程序是:
package shareit.ftp;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;class Node
{
File[] fle;
}
class FtpConnection extends Thread
{ //private String user;
//private String password;
private FtpClient ftpclient;
FtpConnection()
{
try{ftpclient=new FtpClient("192.168.0.66",21);
Login();
}
catch(Exception e){}
}
public void Login()
{
try {
ftpclient.login("winterzd", "2251780");
} catch (IOException e) {
e.printStackTrace();
}
}
public void Logout()
{
ftpclient.sendServer("QUIT\r\n");
int reply = 0;
try {
reply = ftpclient.readServerResponse();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(reply);
}
public void run()
{
try {
//readirt();
//*********************************************
File file=new File("D:\\2");
File[] filelist=file.listFiles();
int count=filelist.length;
System.out.println(count);
int i=count-1;
while(i>=0)
{
if(filelist[i].isDirectory()==false)
/*{try{
String path=filelist[i].getAbsolutePath();
String[] pathsplit=path.split("\\\\");
int j=pathsplit.length-1;
int m=1;
String pathafter="/";
while(m<=j)
{
if(m<j)pathafter=pathafter.concat(pathsplit[m])+"/";
else pathafter=pathafter.concat(pathsplit[m]);
m++;
}
System.out.println(pathafter);
// pathafter="/test";
ftpclient.sendServer("XMKD" + pathafter + "\r\n");
}
catch (Exception e) {e.printStackTrace();}
}
else{*/
try{
String path=filelist[i].getParent();
String[] pathsplit=path.split("\\\\");
int j=pathsplit.length-1;
int m=1;
String pathafter="/";
while(m<=j)
{
if(m<j)pathafter=pathafter.concat(pathsplit[m])+"/";
else pathafter=pathafter.concat(pathsplit[m]);
m++;
}
ftpclient.cd(pathafter);
String name=filelist[i].getName(); //File f=new File(path,name);
ftpclient.binary();
FileInputStream input=new FileInputStream(filelist[i]);
TelnetOutputStream output=ftpclient.put(name);
int bytesRead;
while((bytesRead=input.read())!=-1)
output.write(bytesRead);
input.close();
output.close();
}
catch (Exception e) {e.printStackTrace();}
//}
i--;
}
//**********************************************
}
catch(Exception ee){}
Logout();
}
}public class FTPtxt {
public static void main(String args[])
{
FtpConnection ftp=new FtpConnection();
ftp.start();
}
}
解决方案 »
- 关于effective java2 第66条的程序
- java研究中遇到的问题
- 监听的问题!!谢谢!!
- jboss and tomcat -> applet 显示
- 我在frame1创建了frame2,在创建frame2的时候,frame1.setEnable(false),现在关闭frame2的时候想把frame1.setEnable(true),但是在frame2中
- 在运行一个应用程序时出的问题,请指教,急!!
- applet问题,棘手
- 别人给我一个经过编码的字符串,我要如何知道他是什么编码呀?
- Java里有什么函数可以直接调用win\command下的命令?
- 各位大神帮忙看个java 多线程的程序
- 如何把ResultSet放到jtable中??
- 帮忙,万分感谢
import java.io.File;public class test2 {
public test2() {
File f=new File("d:\\temp");
String path=f.getAbsolutePath();
String list[]=f.list(); for (int i=0;i<list.length;i++)
{
File f2=new File(path+"\\"+list[i]); if (f2.isDirectory()==true)
{
this.showlist(path+"\\"+list[i]);
}
else
{
System.out.println(path+"\\"+list[i]);
}
}
} public void showlist(String path){
File f3=new File(path);
String path2=f3.getAbsolutePath();
String list2[]=f3.list(); for(int j=0;j<list2.length;j++)
{
File ff=new File(path2+"\\"+list2[j]);
if(ff.isDirectory()==true)
{
this.showlist(path+"\\"+list2[j]);
}
else
{
System.out.println(path2+"\\"+list2[j]);
}
}
} public static void main(String[] args) {
test2 test21 = new test2();
}
}
===
在ftpclient.sendServer("XMKD" + pathafter + "\r\n");之后加上这句:
ftpclient.readServerResponse();