java 直接调用python脚本,并传递参数代码实例

2025-05-29 0 99

最近陆续用python写了很多文件处理脚本,虽然功能都比较简单 ,但还是感觉到python对文件处理的简洁高效 ,越发觉得java的语法相当的繁琐~

接到个需求处理ftp数据接口 。所以想把python脚本也用上。java代码定时扫描ftp数据仓库 ,调用python脚本入库。

直接采用java执行系统命令的方式

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36
@async

public void readfilebypython(list<string> filepaths) throws filenotfoundexception {

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<filepaths.size() ;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("<error>");

while((line = br.readline())!=null){

system.out.println(line);

system.out.println("</error>");

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.class 文件路径下的 pythonfilehandle.py 脚本 ,并传入string数组类型的参数(需要处理的文件全路径)

pythonfilehandle脚本接受java传入的文件路径参数(数组),解析并入库

pythonfilehandle.py 代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53
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,latn_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.close

sys.argv[0] 存储的是py文件自身的路径,故接受参数从sys.argv[1]开始。

以上所述是小编给大家介绍的java调用python脚本传递参数详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java 直接调用python脚本,并传递参数代码实例 https://www.kuaiidc.com/109275.html

相关文章

发表评论
暂无评论