public String download(String ftpUrl,String sfzh){
FTPClient ftpClient = new FTPClient();
InputStream inputStream = null;
String re=null;
try {
ftpClient.connect(ftp_ip,ftp_port);
ftpClient.login(ftp_username, ftp_password);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//是否成功登录服务器
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
}
//跳转到指定目录
ftpClient.changeWorkingDirectory(ftpUrl);
//5.遍历下载的目录
FTPFile[] fs = ftpClient.listFiles();
for (FTPFile ff : fs){
//解决中文乱码问题,两次解码
byte[] bytes=ff.getName().getBytes("iso-8859-1");
String fileName=new String(bytes,"utf-8");
if(sfzh.equals(fileName)){
inputStream = ftpClient.retrieveFileStream(fileName);
}
}
if (inputStream != null) {
byte[] data=null;
data=new byte[inputStream.available()];
BASE64Encoder encoder=new BASE64Encoder();
re=encoder.encode(data);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
if(null != inputStream){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return re;
}