当前位置: 移动技术网 > IT编程>开发语言>Java > java 直接调用python脚本,并传递参数代码实例

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

2019年07月19日  | 移动技术网IT编程  | 我要评论
最近陆续用python写了很多文件处理脚本,虽然功能都比较简单 ,但还是感觉到python对文件处理的简洁高效 ,越发觉得java的语法相当的繁琐~ 接到个需求处理ftp

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

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

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

@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 代码

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脚本传递参数详解整合,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网