spring boot 调用python_在嵌入式Tomcat上从Spring-Boot Web应用程序运行Python脚本

I am trying to make a web-app that takes input from the user using HTML Form on Spring-boot (Which is running on embedded Tomcat); I need to pass this input to a Python script sitting somewhere on my hard disk as command-line arg.

Here is my controller that fetches the data from HTML form (Getting data from the form is working fine.)

@Controller

public class PortalController {

@RequestMapping(value="")

public String hello(){

return "portal/welcome";

}

@RequestMapping(value="",method=RequestMethod.POST)

public String addAObjectForm(@RequestParam String val) throws IOException {

String cmd = "python Fetch.py \""+val+"\"";

System.out.println(cmd);

Runtime.getRuntime().exec(cmd);

return "redirect:/";

}

}

The variable 'cmd' stores this value finally: python Fetch.py "Command-line-arg-from-html"

The Python code is scraping web. The code is so big that it cannot be ported to Java. And my v. sweet teacher has asked me to make the web-app on Spring-Boot. Since the app is running on Tomcat, it doesn't look like I will be able to execute the Py script directly as if it were on console. Can someone suggest a way of doing it, please? Any bit of help is highly appreciated.

Note: Both, Spring-Boot Project and Python Script, are working standalone without issues.

The issue I am having is, the code just prints the variable 'cmd' on the console and continues. No changes are reflected in the database (which are supposed to be done by the Py script), it indicates that the script did not execute.

Here is the console window:

2018-03-03 05:44:47.733 INFO 10752 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup

2018-03-03 05:44:47.771 INFO 10752 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''

2018-03-03 05:44:47.771 INFO 10752 --- [ restartedMain] org.OpenLyrics.Portal.PortalApplication : Started PortalApplication in 3.161 seconds (JVM running for 7.414)

2018-03-03 05:44:55.737 INFO 10752 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'

2018-03-03 05:44:55.738 INFO 10752 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started

2018-03-03 05:44:55.765 INFO 10752 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 26 ms

python Fetch.py "Argument 1"

python Fetch.py "Argument 2"

python Fetch.py "Argument 3"

解决方案

I dug the internet a lot and found the solution.

One can do this using the following commands:

String fetching = "python " + "c:\\Fetch.py \"" + songDetails + "\"";

String[] commandToExecute = new String[]{"cmd.exe", "/c", fetching};

Runtime.getRuntime().exec(commandToExecute);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值