java执行shell命令
package com.pms.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
?* 執行 linux命令
?*
?* @author mars
?*
?*/
public class ExecuteLinux {
?// log4j實例名
?private static final Log log = LogFactory.getLog(ExecuteLinux.class
???.getCanonicalName());
?/**
? * 執行linux命令,并輸出結果
? *
? * @param cmd
? * @throws RuntimeException
? * @throws IOException
? */
?public static void execute(String[] cmd) throws Exception {
??InputStream errorStream = null;
??InputStream inputStream = null ;
??Process ps = null;
??try {
???// String[] cmd = new String[] { "/bin/sh", "-c", "ls" };
???ps = Runtime.getRuntime().exec(cmd);
???log.debug("開始執行本地線程");
???
???errorStream = ps.getErrorStream(); // 正確結果的流
???inputStream = ps.getInputStream(); // 錯誤結果的流
???printError(errorStream);
???printInfo(inputStream);
???
??} catch (Exception e) {
???log.error("execute linux command error...", e);
???throw new Exception("execute linux command error...");
??}finally{
???if(errorStream != null){
????errorStream.close();
???}
???if(inputStream != null){
????inputStream.close();
???}
???
???ps.destroy();
??}
??
?}
?
?/**
? * 打印? 正確信息
? * @param errorStream
? * @throws IOException
? */
?private static void printInfo(InputStream inputStream) throws IOException{
??
??StringBuffer sb = new StringBuffer();
??int line=0;
??while ((line = inputStream.read(new byte[1024])) != -1) {
???sb.append((char)line).append("\n");
??}
??String result = sb.toString();
??System.out.println("#####正確信息######:"+result);
??log.debug("#####正確信息LOG######:"+result);
?}
?
?/**
? * 打印錯誤信息
? * @param errorStream
? * @throws IOException
? */
?private static void printError(InputStream errorStream) throws IOException{
??
??StringBuffer sb = new StringBuffer();
??int line=0;
??while ((line = errorStream.read(new byte[1024])) != -1) {
???sb.append((char)line).append("\n");
??}
??String result = sb.toString();
??System.out.println("#####錯誤信息######:"+result);
??log.debug("#####錯誤信息LOG######:"+result);
?}
?
?/**
? * 獲得 wgrib2 切割的linux命令,將grib2切割為二進制 bin 文件
? * @param pathName 文件路徑名
? * @param fileName 文件名
? * @param savePath 要保存到的路徑名
? * @param lists 條件集合(必不為空,因為從數據庫獲得,而存入數據庫時,前臺有校驗不為空)
? * @return
? */
?public static String[] getLinuxCommand(String pathName, String fileName, String savePath, List<String> lists){
??
??//"/bin/sh", "-c",
??String[] cmd = new String[3];
??cmd[0] = "/bin/sh";
??cmd[1] = "-c";
??
??StringBuffer condition_bf = new StringBuffer();
??for (String str : lists) {
???condition_bf.append(str+" mb|");
??}
??// 將最后一個 '|' 符號截掉
??String condition_str = condition_bf.toString().substring(0, condition_bf.toString().length()-1);
??//wgrib2 -s Z_NAFP_C_BABJ_20120508000000_P_CNPC-T639-GMFS-HNEHE-00300.grib2 | grep -E "(\bHGT:20\b)|(\bHGT:30\b)" | wgrib2 -i Z_NAFP_C_BABJ_20120508000000_P_CNPC-T639-GMFS-HNEHE-00300.grib2 -no_header -bin data5.bin
??StringBuffer bf = new StringBuffer();
??bf.append("wgrib2 -s ");
??bf.append(pathName+"/"+fileName);
??bf.append(" | grep -E ");
??bf.append("\""+condition_str+"\"");
??bf.append(" | wgrib2 -i ");
??bf.append(pathName+"/"+fileName);
??bf.append(" -no_header -bin ");
??String newName = fileName.substring(0, fileName.indexOf(".")); // 去掉 .grib2 的文件名
??bf.append(savePath+"/"+newName+".bin");
??cmd[2] = bf.toString();
??
??return cmd;
?}
?
?public static void main(String[] args) {
//??String str = "new.grib2";
//??System.out.println(str.substring(0, str.indexOf(".")));
//??
//??String pathName = "/home/ftp/cwfs";
//??String fileName = "new_003.grib2";
//??String savePath ="/home/ftp/cwfs";
//??
//??List<String> lists = new ArrayList<String>();
//??lists.add("HET:20");
//??String[] command = getLinuxCommand(pathName, fileName, savePath, lists);
//??
//??System.out.println(command[2]);
??
//??File file = new File("E:temp");
//??System.out.println(file.getParent());
//??File[] listFiles = file.listFiles();
//??if(listFiles != null && listFiles.length > 0){
//???for (File f : listFiles) {
//????f.delete();
//???}
//??}
??
//??File[] list = file.listFiles();
//??for (File s : list) {
//???System.out.println(s.getPath());
//??}
??
?}
}
總結
以上是生活随笔為你收集整理的java执行shell命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 你正在开通银联代收业务是什么意思
- 下一篇: CXF+JAXB处理复杂数据