/**
* 登陆FTP服务器
*/
public void login(FTPClient ftpClient) throws Exception {
ReadIniInfo iniInfo = new ReadIniInfo();//读取配置文件对象
String ftpServer = iniInfo.getFtpServer();//获取FTP服务器地址
String ftpPort = iniInfo.getFtpPort();//获取FTP服务器端口
String ftpUser = iniInfo.getFtpUser();//获取FTP用户账号
String ftpPwd = iniInfo.getFtpPwd();//获取FTP用户密码
try {
// 链接到FTP服务器
ftpClient.connect(ftpServer, Integer.valueOf(ftpPort));
System.out.println("链接到FTP服务器:" + ftpServer + "成功..");
// 开始登陆服务器
boolean boo = ftpClient.login(ftpUser, ftpPwd);
if(boo){
System.out.println("登陆到FTP服务器:" + ftpServer + "成功..");
}else{
System.out.println("登陆到FTP服务器:" + ftpServer + "失败..");
logout(ftpClient);//退出/断开FTP服务器链接
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("登陆到FTP服务器:" + ftpServer + "失败..");
}
}
/**
* 将ftp服务器中图片文件信息输出到byte数组中
*
*/
public byte[] getByteArray(String imagePath,FTPClient ftpClient,String fileName) {
byte[] b = null;
BufferedImage image;
ByteArrayOutputStream bos = null;
// TelnetInputStream is=new TelnetInputStream(new FileInputStream(imagePath),);
try {
// 设置以二进制流的方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding("GBK");
ftpClient.changeWorkingDirectory(imagePath);// 转移到FTP服务器目录
FTPFile[] fs = ftpClient.listFiles();//递归目标目录
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {//查找目标文件
image = ImageIO.read(ftpClient.retrieveFileStream(imagePath));
bos = new ByteArrayOutputStream();
javax.imageio.ImageIO.write(image, "png", bos);
b = bos.toByteArray();//generate byte[]
return b;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("目录为空");
e.printStackTrace();
}
finally{
try {
logout(ftpClient);//退出/断开FTP服务器链接
if(null != bos){
bos.close();
}
} catch (IOException e) {
System.out.println("关闭链接失败..");
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
/**
* 将传来的图片byte数组信息保存到远程ftp服务器中
*
*/
public void passBackBmp(FTPClient ftpClient,String str,String fileName){
try {
byte[] b=str.getBytes();
ByteArrayInputStream is = new ByteArrayInputStream(b);
//设置被动模式
ftpClient.enterLocalPassiveMode();// 设置PassiveMode传输
// 设置以二进制流的方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//设置字符集
ftpClient.setControlEncoding("GBK");
ftpClient.changeWorkingDirectory("svc/pic/CZ580013735677/");//改变FTP服务器目录
boolean boo = ftpClient.storeFile(fileName, is);
if(boo){
System.out.println("文件上传成功..");
}else{
System.out.println("文件上传失败..");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
logout(ftpClient);//退出/断开FTP服务器链接
} catch (IOException e) {
System.out.println("关闭链接失败..");
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 退出/断开FTP服务器链接
*/
public void logout(FTPClient ftpClient) throws Exception{
ftpClient.logout();//退出登陆
System.out.println("已退出FTP远程服务器..");
if(ftpClient.isConnected()){
ftpClient.disconnect();//断开链接
System.out.println("已断开FTP服务器链接..");
}
}
更纤细的示例 http://download.youkuaiyun.com/detail/zxy838279821/4241071
FTPjar包 http://download.youkuaiyun.com/detail/zxy838279821/4241053