问题描述
importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importsun.net.TelnetInputStream;importsun.net.TelnetOutputStream;importsun.net.ftp.FtpClient;/***Java自带的API对FTP的操作*@Title:a.java*@author:*/publicclassa{/***本地文件名*/privateStringlocalfilename;/***远程文件名*/privateStringremotefilename;/***FTP客户端*/privateFtpClientftpClient;/***服务器连接*@paramip服务器IP*@paramport服务器端口*@paramuser用户名*@parampassword密码*@parampath服务器路径*@author*@date*/publicvoidconnectServer(Stringip,intport,Stringuser,Stringpassword,Stringpath){try{/*******连接服务器的两种方法*******///第一种方法//ftpClient=newFtpClient();//ftpClient.openServer(ip,port);//第二种方法ftpClient=newFtpClient(ip);ftpClient.login(user,password);//设置成2进制传输ftpClient.binary();System.out.println("loginsuccess!");if(path.length()!=0){//把远程系统上的目录切换到参数path所指定的目录ftpClient.cd(path);}ftpClient.binary();}catch(IOExceptionex){ex.printStackTrace();thrownewRuntimeException(ex);}}/***关闭连接*@author*@date*/publicvoidcloseConnect(){try{ftpClient.closeServer();System.out.println("disconnectsuccess");}catch(IOExceptionex){System.out.println("notdisconnect");ex.printStackTrace();thrownewRuntimeException(ex);}}/***上传文件*@paramlocalFile本地文件*@paramremoteFile远程文件*@author*@date*/publicvoidupload(StringlocalFile,StringremoteFile){this.localfilename=localFile;this.remotefilename=remoteFile;TelnetOutputStreamos=null;FileInputStreamis=null;try{//将远程文件加入输出流中os=ftpClient.put(this.remotefilename);//获取本地文件的输入流Filefile_in=newFile(this.localfilename);is=newFileInputStream(file_in);//创建一个缓冲区byte[]bytes=newbyte[1024];intc;while((c=is.read(bytes))!=-1){os.write(bytes,0,c);}System.out.println("uploadsuccess");}catch(IOExceptionex){System.out.println("notupload");ex.printStackTrace();thrownewRuntimeException(ex);}finally{try{if(is!=null){is.close();}}catch(IOExceptione){e.printStackTrace();}finally{try{if(os!=null){os.close();}}catch(IOExceptione){e.printStackTrace();}}}}publicstaticvoidmain(Stringagrs[]){//Stringfilepath[]={"/temp/aa.txt","/temp/regist.log"};//Stringlocalfilepath[]={"C:\tmp\1.txt","C:\tmp\2.log"};afu=newa();/**使用默认的端口号、用户名、密码以及根目录连接FTP服务器*/fu.connectServer("服务器IP",端口,"用户名","密码","/");//下载//for(inti=0;i<filepath.length;i++){//fu.download(filepath[i],localfilepath[i]);//}Stringlocalfile="E:\号码.txt";Stringremotefile="/temp/哈哈.txt";//上传fu.upload(localfile,remotefile);fu.closeConnect();}}
以上是我的代码,可是报错了~报错代码如下:java.net.ConnectException:Connectionrefused:connectatjava.net.PlainSocketImpl.socketConnect(NativeMethod)atjava.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)atjava.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)atjava.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)atjava.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)atjava.net.Socket.connect(Socket.java:519)atjava.net.Socket.connect(Socket.java:469)atsun.net.NetworkClient.doConnect(NetworkClient.java:163)atsun.net.NetworkClient.openServer(NetworkClient.java:118)atsun.net.ftp.FtpClient.openServer(FtpClient.java:488)atsun.net.ftp.FtpClient.<init>(FtpClient.java:761)ata.connectServer(a.java:47)ata.main(a.java:137)Exceptioninthread"main"java.lang.RuntimeException:java.net.ConnectException:Connectionrefused:connectata.connectServer(a.java:60)ata.main(a.java:137)Causedby:java.net.ConnectException:Connectionrefused:connectatjava.net.PlainSocketImpl.socketConnect(NativeMethod)atjava.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)atjava.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)atjava.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)atjava.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)atjava.net.Socket.connect(Socket.java:519)atjava.net.Socket.connect(Socket.java:469)atsun.net.NetworkClient.doConnect(NetworkClient.java:163)atsun.net.NetworkClient.openServer(NetworkClient.java:118)atsun.net.ftp.FtpClient.openServer(FtpClient.java:488)atsun.net.ftp.FtpClient.<init>(FtpClient.java:761)ata.connectServer(a.java:47)...1more
解决方案
解决方案二:
你这代码是网上copy的吧看看这个
解决方案三:
引用1楼shijing266的回复:
你这代码是网上copy的吧看看这个
是网上copy的,你发的那个,我copy下来,直接报错~需要什么jar包吗?
解决方案四:
那你还是看这个吧,需要一个io包和一个common包
解决方案五:
引用3楼shijing266的回复:
那你还是看这个吧,需要一个io包和一个common包
试了,没报错,但是不成功~
解决方案六:
没大神帮忙解决吗?