多平台统一管理软件接口,如何实现多平台统一管理软件接口
648
2023-01-09
java 直接调用python脚本,并传递参数代码实例
最近陆续用python写了很多文件处理脚本,虽然功能都比较简单 ,但还是感觉到python对文件处理的简洁高效 ,越发觉得java的语法相当的繁琐~
接到个需求处理ftp数据接口 。所以想把python脚本也用上。java代码定时扫描ftp数据仓库 ,调用python脚本入库。
直接采用java执行系统命令的方式
@Async
public void readFileByPython(List
URL localSrcUrl = AbstractReadFileLine.class.getResource("");
String localSrcPath = localSrcUrl.getPath();
localSrcPath = localSrcPath.substring(1, localSrcPath.length());
String pythonFile = localSrcPath + "PythonFileHandle.py";
int size = filePaths.size() + 2;
String[] args = new String[size];
args[0] = "python";
args[1] = pythonFile;
for(int i =0;i int index = i+2; args[index] = filePaths.get(i); } try { System.out.println("start"); Process proc = Runtime.getRuntime().exec(args); InputStream is = proc.getErrorStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line = null; System.out.println(" while((line = br.readLine())!=null){ System.out.println(line); System.out.println(""); int exitValue = proc.waitFor(); System.out.println("Process exitValue="+exitValue); } System.out.println("end"); } catch (Exception e){ e.printStackTrace(); } } String[] args = new String[size]; args[0] = "python"; args[1] = pythonFile; args[0]表示要执行的是python 脚本 ,args[1] 脚本文件的全路径 该方法调用 AbstractReadFileLine.clasfRlThRs 文件路径下的 PythonFileHandle.py 脚本 ,并传入String数组类型的参数(需要处理的文件全路径) PythonFileHandle脚本接受java传入的文件路径参数(数组),解析并入库 PythonFileHandle.py 代码 import pymssql,time,sys reload(sys) sys.setdefaultencoding("utf-8") class MSSQL: def __init__(self,host,user,pwd,db): self.host = host self.user = user self.pwd = pwd self.db = db def __GetConnect(self): if not self.db: raise(NameError,"") self.conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8") cur = self.conn.cursor() if not cur: raise(NameError,"") else: return cur def ExecQuery(self,sql): cur = self.__GetConnect() cur.execute(sql) resList = cur.fetchall() # self.conn.close() return resList def ExecNonQuery(self,sql): cur = self.__GetConnect() cur.execute(sql) self.conn.commit() self.conn.close() def insertToCloseList(data ,ms): sql = "insert into t_isee_closelist_infos (WORK_ORDER_ID,CRM_Cdsc_Id,APPRAISE_TYPE,CRM_Accept_Date,lahttp://tn_code,theme_row_id,TASK_EXECUTE_ROW_ID,CRM_Accept_REASON,ASSET_INTEG_ID) values ( '" temp ="' , '" sqlStr = temp.join(data) sql = sql + sqlStr + "')" ms.ExecNonQuery(sql) ms = MSSQL(host="172.30.0.186",user="sa",pwd="",db="test") fengefu = '$%$' for i in range(1, len(sys.argv)): read = open(sys.argv[i] ,'r') for line in read: line=line.strip('\n') data = line.split(fengefu) insertToCloseList(data,ms) read.http://close sys.argv[0] 存储的是py文件自身的路径,故接受参数从sys.argv[1]开始。 以上所述是给大家介绍的java调用python脚本传递参数详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!
int index = i+2;
args[index] = filePaths.get(i);
}
try {
System.out.println("start");
Process proc = Runtime.getRuntime().exec(args);
InputStream is = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("
while((line = br.readLine())!=null){
System.out.println(line);
System.out.println("");
int exitValue = proc.waitFor();
System.out.println("Process exitValue="+exitValue);
}
System.out.println("end");
} catch (Exception e){
e.printStackTrace();
}
}
String[] args = new String[size];
args[0] = "python"; args[1] = pythonFile; args[0]表示要执行的是python 脚本 ,args[1] 脚本文件的全路径
该方法调用 AbstractReadFileLine.clasfRlThRs 文件路径下的 PythonFileHandle.py 脚本 ,并传入String数组类型的参数(需要处理的文件全路径)
PythonFileHandle脚本接受java传入的文件路径参数(数组),解析并入库
PythonFileHandle.py 代码
import pymssql,time,sys
reload(sys)
sys.setdefaultencoding("utf-8")
class MSSQL:
def __init__(self,host,user,pwd,db):
self.host = host
self.user = user
self.pwd = pwd
self.db = db
def __GetConnect(self):
if not self.db:
raise(NameError,"")
self.conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8")
cur = self.conn.cursor()
if not cur:
raise(NameError,"")
else:
return cur
def ExecQuery(self,sql):
cur = self.__GetConnect()
cur.execute(sql)
resList = cur.fetchall()
#
self.conn.close()
return resList
def ExecNonQuery(self,sql):
cur = self.__GetConnect()
cur.execute(sql)
self.conn.commit()
self.conn.close()
def insertToCloseList(data ,ms):
sql = "insert into t_isee_closelist_infos (WORK_ORDER_ID,CRM_Cdsc_Id,APPRAISE_TYPE,CRM_Accept_Date,lahttp://tn_code,theme_row_id,TASK_EXECUTE_ROW_ID,CRM_Accept_REASON,ASSET_INTEG_ID) values ( '"
temp ="' , '"
sqlStr = temp.join(data)
sql = sql + sqlStr + "')"
ms.ExecNonQuery(sql)
ms = MSSQL(host="172.30.0.186",user="sa",pwd="",db="test")
fengefu = '$%$'
for i in range(1, len(sys.argv)):
read = open(sys.argv[i] ,'r')
for line in read:
line=line.strip('\n')
data = line.split(fengefu)
insertToCloseList(data,ms)
read.http://close
sys.argv[0] 存储的是py文件自身的路径,故接受参数从sys.argv[1]开始。
以上所述是给大家介绍的java调用python脚本传递参数详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~