java调用linux命令(通过ftp连接服务器并给上传的文件赋权限)

aianbailin 2009-11-30 08:54:04
赋权命令:chmod 777 filename
服务器:linux1、linux2。

java程序在linux1上,通过ftp方式将文件上传到linux2上,如何上传后的文件执行chmod赋权语句?

网上搜索到的解决方式:
1、 Runtime.getRuntime().exec("chmod 777 "+filename); ---执行后没有任何反应。
2、调用FtpClient类中sendServer("chmod 777 "+filename+ " ")--执行后仍是没有任何反应。

在线等~
...全文
2703 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangxingxing123 2012-08-17
  • 打赏
  • 举报
回复
学习下,非常有用!
oceanswing 2011-12-15
  • 打赏
  • 举报
回复
也遇到同样的问题
aimujiarui 2010-08-11
  • 打赏
  • 举报
回复
为什么我看不到peng3696的回帖呢?
shmily_zjl 2010-08-10
  • 打赏
  • 举报
回复
mark 刚刚遇到过这种问题,也参照解决了。感谢楼主,感谢peng3696(楚木) ,感谢inkfish(八爪鱼)
lateblue 2010-03-06
  • 打赏
  • 举报
回复
看看!!!!!!!!
墨水鱼 2009-11-30
  • 打赏
  • 举报
回复
看看你的FTPClient是什么类库里的。我看的是commons-net里的FTPClient,有sendCommand这个方法。
aianbailin 2009-11-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 inkfish 的回复:]
第一种楼上已经说了,第二种是ftp命令里没有chmod,这是linux的系统命令
[/Quote]
我通过本机的命令行(cmd)同样ftp连接到linux2服务器,则提示我chmod是无效的命令。
但我使用的是SecureCRT软件,登陆linux1,然后ftp连接linux2,可以使用chmod命令。
也不能说是由于软件才可以使用这个命令的啊!
您说是吧?
墨水鱼 2009-11-30
  • 打赏
  • 举报
回复
请无视3楼的。
用FTPClient.sendCommand(String command, String args)来调用
墨水鱼 2009-11-30
  • 打赏
  • 举报
回复
第一种楼上已经说了,第二种是ftp命令里没有chmod,这是linux的系统命令
aianbailin 2009-11-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zk_2000 的回复:]
第一种方式肯定是不行的。java程序在linux1上运行命令改变linux2上的文件属性肯定是没戏。
第二种我没用过FtpClient,chmod命令本身是可以用FTP改变权限的。
[/Quote]

1、我是通过ftp连接linux2服务器上传文件文件之后调用的这句话。
2、如果可以改变linux2上的权限那如何处理呢?
  • 打赏
  • 举报
回复
第一种方式肯定是不行的。java程序在linux1上运行命令改变linux2上的文件属性肯定是没戏。
第二种我没用过FtpClient,chmod命令本身是可以用FTP改变权限的。
aianbailin 2009-11-30
  • 打赏
  • 举报
回复
OK,结贴,问题解决,感谢peng3696(楚木) 的帮助,感谢inkfish(八爪鱼) 的帮助。
按照楚木的完全解决了问题了。
墨水鱼 2009-11-30
  • 打赏
  • 举报
回复
把ftpClient.sendCommand("chmod 644 " + filename); 换成ftpClient.sendCommand("chmod", 644 " + filename); 怎样?毕竟chmod是command,args是644+filename。
白云冰河 2009-11-30
  • 打赏
  • 举报
回复
FTP.sendCommand是The FTPCommand constant corresponding to the FTP command to send.
你要使用chmod,必须服务器能支持"site chmod"命令和使用FTP.sendSiteCommand
aianbailin 2009-11-30
  • 打赏
  • 举报
回复
静待高手解决中,没人顶,自己顶一下!~
aianbailin 2009-11-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 inkfish 的回复:]
看看你的FTPClient是什么类库里的。我看的是commons-net里的FTPClient,有sendCommand这个方法。
[/Quote]

我下载了您说的这个包,改造了一下方法,上传文件之后我写了一句ftpClient.sendCommand(chmod 644 filename),没有报错,文件上传是成功了,但权限没有变化。以下是我的部分测试代码:
连接ftp服务器代码:
public void connectServer(String server, int port, String user,
String password, String path) throws SocketException, IOException {
ftpClient = new FTPClient();
ftpClient.connect(server, port);
ftpClient.login(user, password);
if (path.length() != 0) {
ftpClient.changeWorkingDirectory(path);
}
}


上传文件方法:
public boolean uploadFile(String fileName, String newName)
throws IOException {
boolean flag = false;
InputStream iStream = null;
try {
iStream = new FileInputStream(fileName);
flag = ftpClient.storeFile(newName, iStream);

} catch (IOException e) {
flag = false;
return flag;
} finally {
if (iStream != null) {
iStream.close();
}

}
ftpClient.sendCommand("chmod 644 " + filename);
return flag;
}
测试代码:
public static void main(String[] args) throws SocketException, IOException {
FtpBean ftp = new FtpBean();
ftp.connectServer("10.1.1.1",21,"admin","admin","/home/test");
ftp.uploadFile("D:\\testFileName","testFileName");
}

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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