|
|
|
package ftp.jdk;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import sun.net.TelnetInputStream;import sun.net.ftp.FtpClient;import ftp.FtpUser;/** * JavaJDK自带ftp操作 * @author cKF29080 * @version [版本号, Nov 24, 2010] * @see [相关类/方法] * @since [产品/模块版本] */public class FtpJdk { private FtpClient ftpClient =null; public boolean loginFtp(String path) throws Exception { boolean isSuccess = false; Properties properties = new Properties(); try { File file=new File(path); InputStream fileInputStream=new FileInputStream(file); properties.load(fileInputStream); String ip = properties.getProperty("Ip"); String name = properties.getProperty("Ip"); String ps = properties.getProperty("Ip"); // saveDir = new String(saveDir.getBytes("ISO-8859-1"),"UTF-8"); fileInputStream.close(); ftpClient = new FtpClient(ip); String str = ftpClient.getResponseString(); System.out.println("log: "+str); ftpClient.login(name,ps); isSuccess = true; } catch (Exception ex) { throw new Exception("Connect ftp server error:" + ex.getMessage()); } return isSuccess; } /** * 下载文件 * @param remotePath ftp目录 * @param localPath 下载本地目录 * @param filename 下载文件名称 * @return [参数说明] * @return boolean [返回类型说明] * @exception throws [违例类型] [违例说明] * @see [类、类#方法、类#成员] */ public boolean downloadFile(String remotePath,String localPath, String filename) { try { if (remotePath.length() == 0){ return false; } /**更改目录*/ ftpClient.cd(remotePath); String str = ftpClient.getResponseString() ; System.out.println("log:"+str) ; /****设置为 二进制输出***/ ftpClient.binary(); /***获得文件流***/ TelnetInputStream is = ftpClient.get(filename); /**构造本地路径**/ File file =new File(localPath); if (!file.exists()) { file.mkdirs(); } File file_out = new File(file, filename); /**输出流**/ FileOutputStream os = new FileOutputStream(file_out); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); } is.close(); os.close(); ftpClient.closeServer(); } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } return true; }} |
|