1.准备脚本 test.sh
2.编写代码
public class DemoTest {
public static void main(String[] args) {
String result=null;
String filePathOne="/home/one/test.pdf";
String filePathTwo="/home/two/test.pdf";
String fileId="123456";
StringBuffer sb =null;
String[] str=new String[4];
str[0]="/home/test/test.sh";
str[1]=filePathOne;//含有引号也需如此,若是"""+filePathOne+"""此种转义写法,会报错,原因不详。
str[2]=filePathTwo;
str[3]=fileId;
Process ps = null;
try {
ps = Runtime.getRuntime().exec(str);
BufferedReader br = new BufferedReader(new InputStreamReader( ps.getInputStream())); sb = new StringBuffer(1024); String line; while ((line = br.readLine()) != null) { sb.append(line); } result = sb.toString(); ps.waitFor(); } catch (Exception e) { result = "-1"; } finally { if(ps != null) { try { ps.getInputStream().close(); ps.getErrorStream().close(); ps.getOutputStream().close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ps.destroy(); } } }
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于