package nssol.util; import sun.net.ftp.*; import sun.net.*; import java.io.*; import javax.swing.*; import java.util.*; public class FTP { public FTP() { } private static FtpClient m_client; protected static void disconnect() { if (m_client != null) { try { m_client.closeServer(); } catch (IOException ex) { } m_client = null; } } protected static boolean connect(String sHost, String user, String password ,String sDir) { try { m_client = new FtpClient(sHost); m_client.login(user, password); m_client.cd(sDir); m_client.binary(); } catch (Exception ex) { return false; } return true; } protected static boolean putFiletoServer(String m_sLocalFile,String m_sHostFile) { if (m_sLocalFile.length()==0) { return false; } byte[] buffer = new byte[10240]; try { File f = new File(m_sLocalFile); int size = (int)f.length(); FileInputStream in = new FileInputStream(m_sLocalFile); OutputStream out = m_client.put(m_sHostFile); int counter = 0; while(true) { int bytes = in.read(buffer); if (bytes < 0) break; out.write(buffer, 0, bytes); counter += bytes; } out.close(); in.close(); } catch (Exception ex) { return false; } return true; } protected static boolean renameFileFromServer(String src, String dst) { try { m_client.rename(src, dst); } catch (Exception ex) { return false; } return true; } public static boolean putFile(String pathname,String ftpServer, String ftpUser, String ftpPasswd, String ftpPath) { String filename; String tmpPathName; String tmpFileName; // step 0 : init { File f = new File(pathname); filename = f.getName(); tmpPathName = pathname + ".tmp"; tmpFileName = filename + ".tmp"; //System.out.println(pathname); //System.out.println(filename); //System.out.println(tmpPathName); //System.out.println(tmpFileName); } // step 1 : rename local filename { File f = new File(pathname); File g = new File(tmpPathName); if (!f.renameTo(g)) return false; }
if (!connect(ftpServer,ftpUser,ftpPasswd,ftpPath)) { return false; } // step 2 : ftping if (!putFiletoServer(tmpPathName,tmpFileName)) { return false; } // step 3 : rename server name if (!renameFileFromServer(tmpFileName,filename)) { return false; } // step 4 : rename local name { File f = new File(tmpPathName); File g = new File(pathname); if (!f.renameTo(g)) return false; } disconnect(); return true;
} /* sample */
public static void main(String[] args) { boolean b = putFile("c:/2.txt","192.168.2.79","dexin","123456","html"); System.out.println(b); }
import sun.net.ftp.*;
import sun.net.*;
import java.io.*;
import javax.swing.*;
import java.util.*;
public class FTP { public FTP() {
} private static FtpClient m_client; protected static void disconnect()
{
if (m_client != null)
{
try
{
m_client.closeServer();
}
catch (IOException ex)
{
} m_client = null;
}
} protected static boolean connect(String sHost, String user,
String password ,String sDir)
{
try
{
m_client = new FtpClient(sHost);
m_client.login(user, password);
m_client.cd(sDir);
m_client.binary();
}
catch (Exception ex)
{
return false;
} return true;
} protected static boolean putFiletoServer(String m_sLocalFile,String m_sHostFile)
{
if (m_sLocalFile.length()==0)
{
return false;
} byte[] buffer = new byte[10240]; try
{
File f = new File(m_sLocalFile);
int size = (int)f.length();
FileInputStream in = new FileInputStream(m_sLocalFile);
OutputStream out = m_client.put(m_sHostFile); int counter = 0;
while(true)
{
int bytes = in.read(buffer);
if (bytes < 0)
break;
out.write(buffer, 0, bytes);
counter += bytes;
} out.close();
in.close();
}
catch (Exception ex)
{
return false;
} return true;
} protected static boolean renameFileFromServer(String src, String dst)
{
try
{
m_client.rename(src, dst);
}
catch (Exception ex)
{
return false;
}
return true;
} public static boolean putFile(String pathname,String ftpServer, String ftpUser,
String ftpPasswd, String ftpPath)
{
String filename;
String tmpPathName;
String tmpFileName; // step 0 : init
{
File f = new File(pathname);
filename = f.getName(); tmpPathName = pathname + ".tmp";
tmpFileName = filename + ".tmp"; //System.out.println(pathname);
//System.out.println(filename);
//System.out.println(tmpPathName);
//System.out.println(tmpFileName); } // step 1 : rename local filename
{
File f = new File(pathname);
File g = new File(tmpPathName); if (!f.renameTo(g))
return false;
}
if (!connect(ftpServer,ftpUser,ftpPasswd,ftpPath))
{
return false;
} // step 2 : ftping
if (!putFiletoServer(tmpPathName,tmpFileName))
{
return false;
} // step 3 : rename server name
if (!renameFileFromServer(tmpFileName,filename))
{
return false;
} // step 4 : rename local name
{
File f = new File(tmpPathName);
File g = new File(pathname); if (!f.renameTo(g))
return false;
} disconnect();
return true;
}
/* sample */
public static void main(String[] args)
{
boolean b = putFile("c:/2.txt","192.168.2.79","dexin","123456","html");
System.out.println(b);
}
}//这是一个利用sun.net.ftp.*包实现FTP的例子,你可以看看,具体的再查找一下帮助