这是我的代码
FtpClient ftpClient = null;
OutputStream os = null;
FileInputStream is = null;
public void connectServer(String ip, int port, String username,String password, String path){
try{/*连接服务器的两种方法*/
ftpClient = FtpClient.create();
try{
SocketAddress addr = new InetSocketAddress(ip,port);
ftpClient.connect(addr);
ftpClient.login(username, password.toCharArray());
System.out.println("登陆成功!");
if(path.length() != 0){
//吧远程系统上的目录切换到参数path所指定的目录
ftpClient.changeDirectory(path);// throws FtpProtocolException, IOException
}
}catch(FtpProtocolException e){
//TODO Auto-generated catch block
e.printStackTrace();
System.out.println(0000000);
}
}catch(IOException ex){
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
public void download(String remoteFile,String localFile){
InputStream is = null;
FileOutputStream fos = null;
System.out.println(111111111);
try{
ftpClient = FtpClient.create();
//获取远程机器上的文件filename,借助TelnetInputStream
try{
is = ftpClient.getFileStream(remoteFile);
}catch(FtpProtocolException e){
//TODO Auto-generated catch block
e.printStackTrace();
}
//File file_in = new File(localFile);
fos = new FileOutputStream(localFile);//localFile 为内存输出流的输出对象
byte[] bytes = new byte[204800];
int c;
while((c = is.read(bytes)) != -1){
fos.write(bytes, 0, c);
}
System.out.println("download success!");
}catch(IOException ex){
System.out.println("not download");
ex.printStackTrace();
throw new RuntimeException(ex);
}finally{
try{
if(is != null){
is.close();
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(fos != null){
fos.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}