java 中应用FTP的一些知识点及注意
主要有ftp的上传、下载、取得ftp文件列表功能。其中上传引用的是jdk自带的sun.net.flt.FtpClient(路径不知时候正确),文件列表和下载应用的是apache 的common -net .jar? jar包
具体代码如下:
/** * 连接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 blocke.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 blocke.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 blocke1.printStackTrace();} catch (IOException e1) {// TODO Auto-generated catch blocke1.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;?}
?