日期:2014-05-16  浏览次数:20569 次

java调用linux 命令

java 在linux环境下调用 linux命令

/**
	 * 完成解压给定的数据包功能
	 * @param ftpDataDir
	 * @param fileName
	 * @return 返回ok 表示解压完成 返回""表示减压失败
	 */
	public String decompressFile(String ftpDataDir,String fileName){
		String results = "";
		BufferedReader in = null;
		try {
			File file = new File(ftpDataDir,fileName);
			boolean isFile = file.isFile();
			if (isFile) {
				// 将fileName减压到ftpDataDir目录下 x文件完整路径释放 ,-r递归子目录,-o解压的目录,-aoa 覆盖已经存在的
				String fileNameS = fileName.substring(0, fileName.lastIndexOf("."));
				String commands = "7za x  "+ftpDataDir+""+fileName+" -r -o"+ftpDataDir+""+fileNameS+" -aoa";
				log.info("解压缩文件命令:"+commands);
				Runtime runtime = Runtime.getRuntime();
				Process process = runtime.exec(commands);
				log.info(commands);
				//in = new BufferedReader(newprocess.getInputStream());
				in = new BufferedReader(new InputStreamReader(process.getInputStream()));
				String str = null;
		        String[] strArray = null;
		        String isOk = "Everything is Ok";
		        while ((str = in.readLine()) != null) {
		        	// 如果返回 "Everything is Ok" 表示减压成功
		        	log.info("---"+str);
		        	if (str.equals(isOk)) {
		        		results = "ok";
		        		return results;
					}
		        }
			}else{
				// 不是一个标准文件
				log.info(ftpDataDir+fileName+"不是一个标准文件");
			}

		} catch (Exception e) {
			System.out.println("解压给定的数据包功能出错!");
			log.error("解压给定的数据包功能出错!:"+ftpDataDir+File.separator+fileName);
			e.printStackTrace();
		}finally{
			if (in != null ) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return results;
	}

?

?

/**
	 * 完成备份数据包功能,即把fileName从ftpDataDir目录备份到backupDir目录
	 * @param ftpDataDir
	 * @param fileName
	 * @param backupDir
	 * @return   返回ok 表示解压完成 返回""表示减压失败
	 */
	public String backupFile(String ftpDataDir,String fileName,String backupDir){
		String results = "";
		BufferedReader in = null;
		try {
			File file = new File(ftpDataDir,fileName);
			boolean isFile = file.isFile();
			if (isFile) {

				Runtime runtime = Runtime.getRuntime();

				// 先复制 再删除 cp -adfpr /var/ftp/test7z/FileUtil.java /var/ftp/xinhua/xinhua/
				String cpCommand = "cp -adfpr "+ftpDataDir+""+fileName+" "+backupDir;
				log.info("备份命令:"+cpCommand);
				Process cpProcess = runtime.exec(cpCommand);
				System.out.println(cpCommand);
				// 验证是否复制成功
				String lsCommand = "ls "+backupDir+""+fileName;
				Process lsProcess = runtime.exec(lsCommand);
				log.info(lsCommand);

				//in = new BufferedReader(newprocess.getInputStream());
				in = new BufferedReader(new InputStreamReader(lsProcess.getInputStream()));
				String str = null;
		        while ((str = in.readLine()) != null) {
		        	// 如果返回 fileName 表示已经移动到备份的目录了
		        	System.out.println("---"+str);
		        	if (str.equals(backupDir+""+fileName)) {
		        		results = "ok";
		        		break;
					}
		        }
		        if ("ok".equals(results)) {
					// 备份成功 删除ftp目录中的文件
		        	//String rmCommand = "rm -f "+ftpDataDir+""+fileName;
					//Process rmProcess = runtime.exec(rmCommand);
					//log.info(rmCommand);
					deleteFile4Linux(ftpDataDir+""+fileName);

					// 删除我们减压缩 出来的文件夹及其文件
					String fileNameS = fileName.substring(0, fileName.lastIndexOf("."));
					//String rmCommand2 = "rm -rf "+ftpDataDir+""+fileNameS;
				//	Process rmProcess2 = runtime.exec(rmCommand2);
					deleteFile4Linux(ftpDataDir+""+fileNameS);
					//log.info(rmCommand2);

				}else {
					results="Failure";
				}
			}else{
				// 不是一个标准文件
				log.info("不是一个标准文件");
			}

		} catch (Exception e) {
			System.out.println("备份文件出错:"+ftpDataDir+"/"+fileName);
			log.error("备份文件出错!:"+ftpDataDir+"/"+fileName);
			e.printStackTrace();
		}finally{
			if (in != null ) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return results;
	}

?