国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java與Python之間使用jython工具類實現數據交互

Java與Python之間使用jython工具類實現數據交互

2021-07-19 09:10texture_texture Java教程

今天小編就為大家分享一篇關于Java與Python之間使用jython工具類實現數據交互,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

最近有個功能需要java與python之間的數據交互,java需要把參數傳給python,然后python計算的結果返回給java.于是就寫了一個工具類.

首先,maven 需要加載jython的依賴.工具類代碼如下:

?
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.util.list;
import java.util.map;
import java.util.properties;
import org.apache.poi.ss.formula.functions.t;
import org.python.core.pyfunction;
import org.python.core.pyinteger;
import org.python.core.pyobject;
import org.python.core.pystring;
import org.python.util.pythoninterpreter;
/** 
 * @classname: jythonutils 
 * @description:todo(jython 工具類) 
 * @author: zy
 * @date:
 *  
 * @copyright: 2018 inc. all rights reserved.
 * 注意:
 */
public class jythonutils {
 /** 
 * @title: jythoninit 
 * @description: todo(初始化jython) 
 * @param: @return  
 * @return: pythoninterpreter  
 * @throws 
 */
 public static pythoninterpreter jythoninit(){
 //初始化site 配置
 properties props = new properties();
    props.put("python.home", ""); //python lib 或 jython lib,根據系統中該文件目錄路徑
    props.put("python.console.encoding", "utf-8");   
    props.put("python.security.respectjavaaccessibility", "false");   
    props.put("python.import.site", "false");
    properties preprops = system.getproperties();
    pythoninterpreter.initialize(preprops, props, new string[0]);
    //創建pythoninterpreter 對象
 pythoninterpreter interp = new pythoninterpreter();
 return interp;
 }
 /** 
 * @title: loadpythonfile 
 * @description: todo(加載python 源碼文件,) 
 * @param: @param interp
 * @param: @param filepath ,比如:f:\\jpython_jar\\jpythontest\\pythontest.py 或/testpython/test.py
 * @param: @return  
 * @return: pythoninterpreter  
 * @throws 
 */
 public static pythoninterpreter loadpythonfile(pythoninterpreter interp, string filepath){
 interp.execfile(filepath);
 return interp;
 }
 /** 
 * @title: loadpythonfunc 
 * @description: todo(加載python 源碼文件中的某個方法) 
 * @param: @param interp
 * @param: @param functionname
 * @param: @return  
 * @return: pyfunction  
 * @throws 
 */
 public static pyfunction loadpythonfunc(pythoninterpreter interp, string functionname){
 //加載方法
   pyfunction func = (pyfunction) interp.get(functionname,pyfunction.class);
 return func;
 }
 /** 
 * @title: execfunc 
 * @description: todo(執行無參方法,返回pyobject) 
 * @param: @param func  
 * @return: pyobject  
 * @throws 
 */
 public static pyobject execfunc(pyfunction func){
 pyobject pyobj = func.__call__();
 return pyobj;
 }
 /** 
 * @title: execfunctostring 
 * @description: todo(執行無參方法,返回一個字符串) 
 * @param: @param func
 * @param: @return  
 * @return: string  
 * @throws 
 */
 public static string execfunctostring(pyfunction func){
 pyobject pyobj = execfunc(func);
 return (string) pyobj.__tojava__(string.class);
 }
 /** 
 * @title: execfunctostring 
 * @description: todo(執行有參方法,返回一個字符串) 
 * @param: @param func
 * @param: @param paramname ,參數名
 * @param: @return  
 * @return: string  
 * @throws 
 */
 public static string execfunctostring2(pyfunction func, string paramname){
 pyobject pyobj = func.__call__(new pystring(paramname));
 return (string) pyobj.__tojava__(string.class);
 }
 /** 
 * @title: execfunctointeger 
 * @description: todo(執行無參方法,返回一個integer) 
 * @param: @param func
 * @param: @return  
 * @return: integer  
 * @throws 
 */
 public integer execfunctointeger(pyfunction func){
 pyobject pyobj = execfunc(func);
 return (integer) pyobj.__tojava__(integer.class);
 }
 /** 
 * @title: execfunctolist 
 * @description: todo(執行無參方法,返回一個list) 
 * @param: @param func
 * @param: @return  
 * @return: list<t>  
 * @throws 
 */
 public list<t> execfunctolist(pyfunction func){
 pyobject pyobj = execfunc(func);
 return (list<t>) pyobj.__tojava__(list.class);
 }
 /** 
 * @title: execfunctomap 
 * @description: todo(執行無參方法,返回一個map<string, object>) 
 * @param: @param func
 * @param: @return  
 * @return: map<string,object>  
 * @throws 
 */
 public map<string, object> execfunctomap(pyfunction func){
 pyobject pyobj = execfunc(func);
 return (map<string, object>) pyobj.__tojava__(map.class);
 }
 public void execfunctobyparamslist(pyfunction func, list<t> paramslist){
 }
 public static void main(string[] args){
 pythoninterpreter interp = jythoninit();
 //文件名
 string filepath = "f:\\jpython_jar\\jpythontest\\pythontest.py";
 interp = loadpythonfile(interp, filepath);
 //函數名
 string functionname = "count";
 pyfunction func = loadpythonfunc(interp, functionname);
 //執行無參方法,返回pyobject
 pyobject pyobj = execfunc(func);
 //執行無參方法,返回string
 string resultstr = execfunctostring(func);
 //執行有參方法,返回string
 string paramname = "name";
 string resultstr2 = execfunctostring2(func, paramname);
 }
}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

原文鏈接:https://blog.csdn.net/cafebar123/article/details/79394431

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日本在线 | 国产一区在线免费观看 | 欧美日韩一区二区三区在线观看 | 国产一区二区三区在线视频观看 | 国产精品第一国产精品 | 久久精品一 | 视频一区 中文字幕 | 成年人视频在线观看免费 | 精品久久av | 精品综合久久久 | 久久久久久久国产精品 | 激情视频网 | 亚洲日韩欧美一区二区在线 | 国产九九九 | 91丁香婷婷综合久久欧美 | 久久精品亚洲精品国产欧美kt∨ | 日日骚视频| 久久精品免费观看 | 国产三级 | 国产成人在线看 | 国产欧美日韩综合精品一区二区 | 性色av一二三杏吧传媒 | 欧美精品在线观看 | 一区不卡 | 久久久国产一区二区三区 | 91色乱码一区二区三区 | 精品国产91亚洲一区二区三区www | 成人精品一区二区三区 | 视频一区二区三区在线观看 | 欧美在线观看一区二区 | 成人欧美一区二区三区在线观看 | 免费a视频在线观看 | 国产超碰人人爽人人做人人爱 | 黄色片在线免费观看 | 91特片网| 日韩欧美中文在线 | 亚洲男女视频在线观看 | 可以在线观看的av网站 | 久久99精品久久久久 | 一区二区久久 | 日韩在线一区二区 |