java调用shell的参数问题

hunsea 2004-10-21 10:01:26
我有一个downfile.sh:
ftp -i -n xxx.xxx.xxx.xxx << EOF
user username password
bin
get /tmp/file1
bye
EOF
下载ftp服务器上的文件file1。一段java程序调用这个shell:Process p= Runtime.getRuntime().exec("./downfile.sh");
现在我想下载别的文件如file2,修改shell,使得可以把要下载的文件名作为参数传给shell,完成下载?
...全文
316 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
YaST 2004-10-22
  • 打赏
  • 举报
回复
import java.io.IOException;
import java.io.InputStream;

/**
*
* excute a shell script
*/
public class Test {

public static void main(String[] args) {
new Test().downFile("filetest");
}

/**
*
* @param file file name to download
*/
public void downFile(String file){
try {
execScript(new String[]{"./downfile",file});
} catch (IOException e) {
System.err.println("Failed to download files"+file+", abort.");
e.printStackTrace();
}
}

/**
* excute shell script with parameters
* @param args string array fill with shell script name and parameters
* @throws IOException
*/
public void execScript(String[] args) throws IOException{
String[] shellArgs=args;
System.arraycopy(new String[]{"/bin/sh"},0,args,0,1);
java.lang.Process myproc = Runtime.getRuntime().exec(shellArgs);
InputStream is = myproc.getInputStream();
byte[] data = new byte[1024*2];
int i = 0;
is.read(data,0,1024*2);
System.out.println(new String(data));
is.close();
is = null;
}
}
YaST 2004-10-21
  • 打赏
  • 举报
回复
明天告诉你,我最近刚做完的类似的东西。

23,117

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧