最近陆续用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网站的支持!
相关文章
猜你喜欢
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 104
-
CentOS下ssh如何登录限制ip?CentOS下ssh登录限制ip的方法
2025-05-27 86 -
2025-05-29 94
-
2025-06-04 73
-
2025-05-27 64
热门评论

