ubantu下安装 apache+mysql+php
2013-11-13 15:32
381 查看
package brips.com.yxjx.pub; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import org.apache.commons.io.FileUtils; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClientConfig; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; public class FTPHandle { private String username; private String password; private String ip; private int port; private Properties property = null;// 配置 private String configFile;// 配置文件的路径名 private FTPClient ftpClient = null; private String filedir = "";// FTP文件路径 private final String[] FILE_TYPES = { "文件", "目录", "符号链接", "未知类型" }; private static SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm"); /** * 设置参数 * * @param configFile * --参数的配置文件 */ private void setArg(String configFile) { property = new Properties(); BufferedInputStream inBuff = null; try { inBuff = new BufferedInputStream(this.getClass().getResourceAsStream(configFile)); property.load(inBuff); username = property.getProperty("username"); password = property.getProperty("password"); ip = property.getProperty("ip"); port = Integer.parseInt(property.getProperty("port")); filedir = property.getProperty("filedir"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (inBuff != null) inBuff.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * 设置FTP客服端的配置--一般可以不设置 * * @return */ private FTPClientConfig getFtpConfig() { FTPClientConfig ftpConfig = new FTPClientConfig( FTPClientConfig.SYST_UNIX); ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING); return ftpConfig; } /** * 连接到服务器 */ public void connectServer() { if (ftpClient == null) { int reply; try { setArg(configFile); ftpClient = new FTPClient(); // ftpClient.configure(getFtpConfig()); ftpClient.connect(ip); ftpClient.login(username, password); ftpClient.setDefaultPort(port); System.out.print(ftpClient.getReplyString()); reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); System.err.println("FTP server refused connection."); } } catch (Exception e) { System.err.println("登录ftp服务器【" + ip + "】失败"); e.printStackTrace(); } } } /** * 进入到服务器的某个目录下 * * @param directory */ public void changeWorkingDirectory() { try { ftpClient.changeWorkingDirectory(filedir); } catch (IOException ioe) { ioe.printStackTrace(); } } /** * 上传文件 * * @param inputStream--文件输入流 * @param newFileName--新的文件名 */ public void uploadFile(InputStream inputStream, String newFileName) { changeWorkingDirectory();// 进入文件夹 // 上传文件 BufferedInputStream buffIn = null; try { buffIn = new BufferedInputStream(inputStream); ftpClient.storeFile(newFileName, buffIn); } catch (Exception e) { e.printStackTrace(); } finally { try { if (buffIn != null) buffIn.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * 列出服务器上文件和目录 * * @param regStr * --匹配的正则表达式 */ @SuppressWarnings("unchecked") public List listRemoteFiles(String regStr) { List list = new ArrayList(); try { FTPFile[] files = ftpClient.listFiles(regStr); if (files == null || files.length == 0) { System.out.println("There has not any file!"); return null; } else { for (FTPFile file : files) { if (file != null) { Map map = new HashMap(); String filename = file.getName(); int filenamelen = filename.length(); if(filenamelen>4){ String filetype = filename.substring(filenamelen-3); if("txt".equals(filetype)){ String name = file.getName(); name = name.substring(0,name.length()-4); map.put("filename", name); map.put("filesize", FileUtils.byteCountToDisplaySize(file.getSize())); map.put("scsj", dateFormat.format(file.getTimestamp().getTime())); list.add(map); } } } } } } catch (Exception e) { e.printStackTrace(); } return list; } /** * 下载文件 * @param remoteFileName --服务器上的文件名 * @param localFileName--本地文件名 */ public void loadFile(String remoteFileName,String localFileName){ //下载文件 BufferedOutputStream buffOut=null; try{ buffOut=new BufferedOutputStream(new FileOutputStream(localFileName)); ftpClient.retrieveFile(remoteFileName, buffOut); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(buffOut!=null) buffOut.close(); }catch(Exception e){ e.printStackTrace(); } } } /** * 删除文件 */ public void deleteFile(String filename){ try{ ftpClient.deleteFile(filename); }catch(IOException ioe){ ioe.printStackTrace(); } } /** * 关闭连接 */ public void closeConnect(){ try{ if(ftpClient!=null){ ftpClient.logout(); ftpClient.disconnect(); System.out.println("Ftp have closed"); } }catch(Exception e){ e.printStackTrace(); } } public String getConfigFile() { return configFile; } public void setConfigFile(String configFile) { this.configFile = configFile; } public String[] getFILE_TYPES() { return FILE_TYPES; } public FTPClient getFtpClient() { return ftpClient; } public void setFtpClient(FTPClient ftpClient) { this.ftpClient = ftpClient; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public Properties getProperty() { return property; } public void setProperty(Properties property) { this.property = property; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getFiledir() { return filedir; } public void setFiledir(String filedir) { this.filedir = filedir; } }
相关文章推荐
- fedora18系统中安装Apache+PHP+MySQL环境
- WIN2003下安装amp(apache+mysql+php+phpmyadmin)环境
- PHP+Apache+MySQL经典搭配,创建环境四 phpMyAdmin安装(转载并修改)
- centos安装配置apache+php+mysql环境
- windows下 apache,php,mysql,phpadmin集成化安装
- linux下安装apache+php+mysql
- centos下yum搭建安装linux+apache+mysql+php环境的方法
- 在windows下安装PHP+MYSQL+APACHE
- Apache,PHP,MySQL的安装,配置
- PHP、mysql、apache安装
- as4上安装apache,mysql,php,cacti,nagios目前都是最新版本上
- LAMP(linux下apache+mysql+php)平台编译安装的实现
- ubuntu下安装Apache+PHP+Mysql
- Apache+php+mysql+SQLyog在windows7下的安装与配置图解
- Centos编译安装 LAMP (apache-2.4.7 + mysql-5.5.35 + php 5.5.8)+ Redis
- WAMP5-v1.7.4 一款Windos Apache Mysql PHP集成安装环境的软件
- CentOS 安装 Apache PHP MYSQL Memcached Memcache 环境配置
- Winodws下IIS/Apache+PHP+MySQL的安装配置
- CentOS 6.3 yum安装LAMP(Apache+MySQL+PHP)
- 在 Windows 下安装 WAMP(Apache、MySQL、PHP)详细图文教程