java调用python接口使用了以下方法 string pythonscriptpathpng = "ruoyi-edu\\src\\main\\java\\com\\gtvote\\edu\\python\\pptutils.py"; processbuilder pbpng = new processbuilder("python", pythonscriptpathpng, pptfilepath, "png"); process ppng = pbpng.start(); // 读取python脚本的输出 bufferedreader in = new bufferedreader(new inputstreamreader(ppng.getinputstream())); string line = null; while ((line = in.readline()) != null) { system.out.println(line); } ppng.waitfor(); // 等待 python 脚本执行完成 但是传入的中文会变成乱码
这个问题可能是由于字符编码不一致导致的。在Java中调用Python脚本时,需要确保两者使用相同的字符编码。可以尝试将Java代码中的字符编码设置为UTF-8,如下所示: ```java String pythonScriptPathPng = "ruoyi-edu\\src\\main\\java\\com\\gtvote\\edu\\python\\pptutils.py"; ProcessBuilder pbPng = new ProcessBuilder("python", pythonScriptPathPng, pptFilePath, "png"); pbPng.environment().put("PYTHONIOENCODING", "UTF-8"); // 设置字符编码为UTF-8 Process pPng = pbPng.start(); // 读取Python脚本的输出 BufferedReader in = new BufferedReader(new InputStreamReader(pPng.getInputStream(), StandardCharsets.UTF_8)); // 使用UTF-8编码读取输入流 String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } pPng.waitFor(); // 等待Python脚本执行完成 ``` 这样应该可以解决中文乱码的问题。 [2024-06-06 17:07:59 | AI写代码神器 | 263点数解答]
相关提问
- [讯飞星火] java调用python接口使用了以下方法 string pythonscriptpathpng = "ruoyi-edu\\src\\main\\java\\com\\gtvote\\edu\\python\\pptutils.py"; processbuilder pbpng = new processbuilder("python", pythonscriptpathpng, pptfilepath, "png"); process ppng = pbpng.start(); // 读取python脚本的输出 bufferedreader in = new bufferedreader(new inputstreamreader(ppng.getinputstream())); string line = null; while ((line = in.readline()) != null) { system.out.println(line); } ppng.waitfor(); // 等待 python 脚本执行完成 但是传入的中文会变成乱码(263点数解答 | 2024-06-06 17:07:59)361
- [讯飞星火] java调用python接口 以下方法 //调用吧ppt转换成图片的py脚本 string pythonscriptpathpng = "ruoyi-edu\\src\\main\\java\\com\\gtvote\\edu\\python\\pptutils.py"; processbuilder pbpng = new processbuilder("python", pythonscriptpathpng, pptfilepath, "png"); process ppng = pbpng.start(); // 读取python脚本的输出 bufferedreader in = new bufferedreader(new inputstreamreader(ppng.getinputstream())); string line = null;(295点数解答 | 2024-06-06 17:07:15)305
- [讯飞星火] java调用python接口使用的 string pythonscriptpathpng = "ruoyi-edu\\src\\main\\java\\com\\gtvote\\edu\\python\\pptutils.py"; processbuilder pbpng = new processbuilder("python", pythonscriptpathpng, pptfilepath, "png"); process ppng = pbpng.start(); // 读取 python 脚本写入的文件 bufferedreader reader = new bufferedreader(new filereader("count.txt")); ppng.waitfor(); 传入的中文变成了乱码(344点数解答 | 2024-06-06 17:19:55)496
- [讯飞星火] java调用python接口使用的 string pythonscriptpathpng = "ruoyi-edu\\src\\main\\java\\com\\gtvote\\edu\\python\\pptutils.py"; processbuilder pbpng = new processbuilder("python", pythonscriptpathpng, pptfilepath, "png"); process ppng = pbpng.start(); // 读取 python 脚本写入的文件 bufferedreader reader = new bufferedreader(new filereader("count.txt")); ppng.waitfor(); java传入的中文值变成了乱码(264点数解答 | 2024-06-06 17:27:09)360
- [讯飞星火] java调用python接口使用的string pythonscriptpathpng = "ruoyi-edu\\src\\main\\java\\com\\gtvote\\edu\\python\\pptutils.py"; processbuilder pbpng = new processbuilder("python", pythonscriptpathpng, pptfilepath, "png"); process ppng = pbpng.start(); // 读取 python 脚本写入的文件 bufferedreader reader = new bufferedreader(new filereader("count.txt")); ppng.waitfor(); 传入的中文是乱码(160点数解答 | 2024-06-06 17:18:39)386
- [字节豆包] [{id:1,src:'/1.jpg'},{id:2,src:'null'},{id:3,src:'/2.jpg'},{id:4,src:'null'},{id:5,src:'/3.jpg'},{id:6,src:'null'}] js转换[{id:1,src:'/1.jpg'},{id:2,src:'/2.jpg'},{id:3,src:'/3.jpg'},{id:4,src:'null'},{id:5,src:'{id:4,src:'null'},'},{id:6,src:'null'}](680点数解答 | 2025-08-04 17:09:03)177
- [阿里通义] public class Animal { public Animal(){ System.out.println("Animal的无参方式被调用"); } public Animal(String name){ System.out.println("Animal的单参构造方法被调用,name=" + name); } public Animal(String name, int age) { System.out.println("Animal的双参构造方法被调用,name=" + name + ", age=" + age); } } public class Dog extends Animal { private String breed; public Dog(){ this.breed=" "; System.out.println("Dog的无参构造方法被调用" ); } public Dog(String na(650点数解答 | 2025-11-09 15:27:37)65
- [DeepSeek] 错误信息 Traceback (most recent call last): File "Main.py", line 19, in <module> main() File "Main.py", line 11, in main N = int(input().strip()) ValueError: invalid literal for int() with base 10: '13 a b c' /1209/4.in: non-zero return = 1 Traceback (most recent call last): File "Main.py", line 19, in <module> main() File "Main.py", line 11, in main N = int(input().strip()) ValueError: invalid literal for int() with base 10: '13 a b c' /1209/5.in: non-zero return (182点数解答 | 2026-01-06 12:34:41)28
- [DeepSeek] Traceback (most recent call last): File "Main.py", line 38, in <module> main() File "Main.py", line 28, in main with open("star.in", "r") as fin: FileNotFoundError: [Errno 2] No such file or directory: 'star.in' /1213/sample.in: non-zero return = 1 Traceback (most recent call last): File "Main.py", line 38, in <module> main() File "Main.py", line 28, in main with open("star.in", "r") as fin: FileNotFoundError: [Errno 2] No such file or directory: 'star.in' /1213/sta(443点数解答 | 2026-01-06 17:52:45)25
- [阿里通义] d:\anaconda3\python.exe c:\users\86139\pycharmprojects\10.6\jd.py traceback (most recent call last): file "c:\users\86139\pycharmprojects\10.6\jd.py", line 49, in <module> main() file "c:\users\86139\pycharmprojects\10.6\jd.py", line 45, in main save_to_mongodb(products) file "c:\users\86139\pycharmprojects\10.6\jd.py", line 40, in save_to_mongodb collection.insert_many(products) file "d:\anaconda3\lib\site-packages\pymongo\_csot.py", line 119, in csot_wrapper return fun(588点数解答 | 2024-12-12 00:27:27)268
- [讯飞星火] Traceback (most recent call last): File "Main.py", line 52, in <module> pattern = generate_pattern(n) File "Main.py", line 6, in generate_pattern prev_pattern = generate_pattern(n - 1) File "Main.py", line 6, in generate_pattern prev_pattern = generate_pattern(n - 1) File "Main.py", line 38, in generate_pattern raise NotImplementedError("Only supports n up to 4 currently") NotImplementedError: Only supports n up to 4 currently /1214/2.in: non-zero return = 1 ----tim(2049点数解答 | 2026-01-07 17:47:12)24
- [字节豆包] class sunflower extends plant{ △ 8 public void show( ){ 9 system.out.println("向日葵"); 10 } 11}12 13 class nut extends plant{ 14 public void show( ){ 15 system.out.println("坚果墙"); 16 17}18 19 class pea extends plant{ 209 public void show( ){ 21 system.out.println("豌豆射手"); 22 } 23 }24 25 class plantfactory{ 26 public static plant creat(string s){ 27 if("向日葵".equalsignorecase(s)){ 28 return new sunflower(); 29 }else if("坚果墙".equalsignorecase(s)){ 30 return new nut(); 31 }else if("豌豆射手".(31点数解答 | 2024-10-17 17:03:18)258