package com.xnh.atom.server;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;
public class FtpUtil {
private static final Logger log = LoggerFactory.getLogger(FtpUtil.class);
private String ip = "";
private String username = "";
private String password = "";
private int port = 21;
private String path = "";
FtpClient ftpClient = null;
OutputStream os = null;
FileInputStream is = null;
public FtpUtil(String serverIP, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
}
public FtpUtil(String serverIP, int port, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
this.port = port;
}
/**
* 连接ftp服务器
*
* @throws IOException ,FtpProtocolException
*/
public void connectServer() throws IOException, FtpProtocolException {
ftpClient = FtpClient.create();
ftpClient.connect(new InetSocketAddress(ip, port));
ftpClient.login(this.username,this.password.toCharArray());
if (this.path.length() != 0){
ftpClient.changeDirectory(this.path);// path是ftp服务下主目录的子目录
}
ftpClient.setBinaryType();// 用2进制上传、下载
System.out.println("已登录到\"" + ftpClient.getWorkingDirectory() + "\"目录");
}
/**
* 断开与ftp服务器连接
*
* @throws IOException
*/
public void closeServer(){
try{
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
if (ftpClient != null) {
ftpClient.close();
}
System.out.println("已从服务器断开");
}catch(IOException e){
e.printStackTrace();
}
}
/**
* 检查文件夹在当前目录下是否存在
* @param dir
* @return
*/
private boolean isDirExist(String dir){
boolean isDir = false;
String pwd = "";
try {
pwd = ftpClient.getWorkingDirectory();
ftpClient.changeDirectory(dir);
ftpClient.changeToParentDirectory();
isDir = true;
}catch(Exception e){
log.error("获取文件出错,文件名:" + dir);
log.error("获取文件出错,原因为:" + e.getMessage(), e);
}finally{
if (pwd!=null) {
try {
ftpClient.changeDirectory(pwd);
} catch (FtpProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return isDir;
}
/**
* 切换目录
*
* @param path
*/
public void changeDirectory(String path) {
try {
ftpClient.changeDirectory(path);
} catch (FtpProtocolException e) {
try {
ftpClient.makeDirectory(path);
System.out.println("文件目录不存在");
} catch (FtpProtocolException ftpProtocolException) {
ftpProtocolException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 上传文件
*
* @param localFile
* @param ftpFile
*/
public void upload(String localFile, String ftpFile) {
OutputStream os = null;
FileInputStream fis = null;
try {
// 将ftp文件加入输出流中。输出到ftp上
os = ftpClient.putFileStream(ftpFile);
File file = new File(localFile);
// 创建一个缓冲区
fis = new FileInputStream(file);
byte[] bytes = new byte[1024];
int c;
while ((c = fis.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
System.out.println("upload success!!");
} catch (FtpProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null)
os.close();
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 上传文件
*
* @param outputStream
* @param ftpFile
*/
public void outUpload(ByteArrayOutputStream outputStream, String ftpFile) {
OutputStream os = null;
FileInputStream fis = null;
try {
os = ftpClient.putFileStream(ftpFile);
os.write(outputStream.toByteArray());
System.out.println("upload success!!");
} catch (FtpProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null)
os.close();
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 上传文件
*
* @param data
* @param ftpFile
*/
public void outUploadTxt(String data, String ftpFile) {
FileInputStream fis = null;
try {
os = ftpClient.putFileStream(ftpFile);
os.write(data.getBytes());
System.out.println("upload success!!");
} catch (FtpProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null)
os.close();
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 文件下载
*
* @param localFile
* @param ftpFile
*/
public void download(String localFile, String ftpFile) {
InputStream is = null;
FileOutputStream fos = null;
try {
// 获取ftp上的文件
is = ftpClient.getFileStream(ftpFile);
File file = new File(localFile);
byte[] bytes = new byte[1024];
int i;
fos = new FileOutputStream(file);
while ((i = is.read(bytes)) != -1) {
fos.write(bytes, 0, i);
}
System.out.println("download success!!");
} catch (FtpProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
JDK1.7之ftp上传文件
最新推荐文章于 2024-05-16 16:03:07 发布