高效的java ftp下载

public static boolean uploadToFtp(String url,int port,String username,String passwd,
String path,String fileName,InputStream inputStream) throws IOException{
boolean flag=false;
FTPClient ftp=new FTPClient();
try {
if(port>-1){
ftp.connect(url,port);
}else{
ftp.connect(url);//ftp默认的端口是21
}
if(ftp.login(username,passwd)){
ftp.enterLocalActiveMode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//创建目录,如果存在会返回失败
ftp.makeDirectory(path);
//切换目录
ftp.changeWorkingDirectory(path);
//上传文件 
//FTP协议规定文件编码格式为ISO-8859-1
fileName=new String(fileName.getBytes("utf-8"),"ISO-8859-1");
OutputStream out=ftp.storeFileStream(fileName);
byte[]byteArray=new byte[4096];
int read=0;
while((read=inputStream.read(byteArray))!=-1){
out.write(byteArray,0,read);
}
out.close();
ftp.logout();
flag=true;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(ftp.isConnected()){
ftp.disconnect();
}
}
return flag;
}

public static boolean downloadFromFtp(String url,int port,String 
username,String password,String path,String fileName,String localpath) throws IOException{
boolean flag=false;
FTPClient ftp=new FTPClient();//org.apache.commons.net.ftp
int reply;
try {
if(port>-1){
ftp.connect(url,port);
}else{
ftp.connect(url);//ftp默认的端口是21
}
//很多人写的是用ftp.getReplyCode()给获取连接的返回值,但是这样会导致storeFileStream返回null
ftp.login(username,password);
ftp.enterLocalActiveMode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply=ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){
ftp.disconnect();
return flag;
}
//切换目录 此处可以判断,切换失败就说明ftp上面没有这个路径
ftp.changeWorkingDirectory(path);
//上传文件 
OutputStream out=null;
InputStream in=null;
//创建本地的文件时候要把编码格式转回来
fileName=new String(fileName.getBytes("ISO-8859-1"),"utf-8");
File localFile=new File(localpath+fileName);
out=new FileOutputStream(localFile);
in=ftp.retrieveFileStream(fileName);
byte[]byteArray=new byte[4096];
int read=0;
while((read=in.read(byteArray))!=-1){
out.write(byteArray,0,read);
}
//这句很重要 要多次操作这个ftp的流的通道,要等他的每次命令完成
ftp.completePendingCommand();
out.flush();
out.close();
ftp.logout();
flag=true;
} catch(Exception e) {
e.printStackTrace();
}finally{
if(ftp.isConnected()){
ftp.disconnect();
}
}
return flag;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值