자바 에서 응용 FTP 의 지식 및 주의
5142 단어 java ftp
구체 적 인 코드 는 다음 과 같다.
/**
* ftp
* @param server
* @param user
* @param password
* @param path
*/
public static FtpClient connectServer(String server, String user, String password,String path){
// server:FTP IP ;user: FTP
// password: FTP ;path:FTP
FtpClient ftpClient = new FtpClient();
try {
ftpClient.openServer(server);
ftpClient.login(user, password);
//path ftp
if (path.length() != 0)
ftpClient.cd(path);
// 2
ftpClient.binary();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ftpClient;
}
/**
* ftp
* @param , 21
* @param user
* @param password
* @param path
* @file
*/
public static boolean uploadToFtp(String server, String user, String password,String path,File file){
// server:FTP IP ;user: FTP
// password: FTP ;path:FTP
boolean flag = true;
FtpClient ftpClient = new FtpClient();
try {
ftpClient.openServer(server);
ftpClient.login(user, password);
//path ftp
if (path.length() != 0)
ftpClient.cd(path);
// 2
ftpClient.binary();
if (ftpClient != null) {
TelnetOutputStream os = null;
FileInputStream is = null;
//"upftpfile" ftp
os = ftpClient.put(file.getName());
is = new FileInputStream(file);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
ftpClient.closeServer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
flag = false;
}
return flag;
}
/**
* FTP
*/
public static List getFtpList(String server, String user, String password,String path){
org.apache.commons.net.ftp.FTPClient ftpClient = new FTPClient();
List<FTPFile> listFtpFiles = new ArrayList<FTPFile>();
try {
ftpClient.connect(server);
ftpClient.login(user, password);
FTPFile[] names = ftpClient.listFiles(path);
// ftpClient.setKeepAlive(true);
for(FTPFile ftpFile :names){
//System.out.println(" :"+new String(ftpFile.getName().getBytes("iso-8895-1"),"gbk"));
ftpFile.setName(new String(ftpFile.getName().getBytes("iso-8859-1"),"gbk"));
listFtpFiles.add(ftpFile);
}
ftpClient.disconnect();
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return listFtpFiles;
}
/**
* ftp
* @param fileName
* @param outDir
*/
public static boolean downLoadFtpFile(String server, String user, String password,String path,String fileName,String outDir){
boolean flag = true;
org.apache.commons.net.ftp.FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server);
ftpClient.login(user, password);
FTPFile[] names = ftpClient.listFiles(path);
// ftpClient.setKeepAlive(true);
for(FTPFile ftpFile :names){
//System.out.println(" :"+new String(ftpFile.getName().getBytes("iso-8895-1"),"gbk"));
ftpClient.setControlEncoding("gbk");
String name = new String(ftpFile.getName().getBytes("iso-8859-1"),"gbk");
// String name = ftpFile.getName();
if(name.equalsIgnoreCase(fileName)){
OutputStream os = new FileOutputStream(new File(outDir+File.separatorChar+name));
System.out.println(" :"+name);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 0kb
ftpClient.retrieveFile(new String((path+File.separatorChar+name).getBytes("gbk"),"iso-8859-1"), os);
os.close();
}
}
ftpClient.disconnect();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
flag = false;
}
return flag;
}