#! /bin/bash
rurl=/data/crm/tomcat/webapps/ROOT/upload/
su - gpguser -c "gpg --recipient XXX --output $rurl$1 --encrypt $rurl$2"
if [ -f "$rurl$1" ]; then
echo "$1"
fi</span>
因gpg加密工具是gpguser安装的,所有用java 调用shell脚本时执行gpg命令会报找不到密钥文件的错误。
可以利用”su - 用户 -c“ 来指定用户执行。
调用shell 的java 代码:
/**
<span style="font-family:KaiTi_GB2312;"> * gpg加密
* @return
*/
private boolean encrypt(String outputname,String inputname){
String line = null;
Process process = null;
try {
String[] cmd = {"/bin/bash", "-c", "/data/crm/tomcat/shell/gpgEncryptFile "+outputname+" "+inputname};
process = Runtime.getRuntime().exec(cmd);
int exitValue = process.waitFor();
if (0 != exitValue) {
logger.info("call shell failed. error code is :" + exitValue);
return false;
}
logger.error("call shell code is :" + exitValue);
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
boolean b=false;
while ((line = input.readLine()) != null) {
if(line.equals(outputname)){
b= true;
}
}
input.close();
return b;
} catch (IOException e) {
e.printStackTrace();
} catch (Throwable e) {
logger.error("call shell failed. " + e);
}
return false;
}</span>