小吕同学真的不会敲代码 2023-02-12 17:33 采纳率: 57.1%
浏览 160
已结题

spring通过flask调用深度学习模型,怎么互相传参?

项目的web端是用spring mvc搭建的,需要调用训练好的图片分类模型,目前java的提交上传图片的controller中用response.sendRedirect将请求重定向到了flask接口,在flask中再利用return redirect( 'http://localhost:8080/user/result?month=%27 + str(monthP) + '&imagePathP=' + str(imagepathP)) 把模型预测后的值monthP返回给web,点击上传图片进行预测后报错:
Request method 'GET' not supported

img

flask用的是GET方法,报了这个错后就企图改成POST方法,但是不知道怎么给java传参 ,试了几次直接报空指针了,感觉还是有点懵,求各位指教这两部分互相传参应该怎么写怎么改动!

以下是目前java和flask部分的写法:

@PostMapping("/user/result")
    public String assessment(Model model,Image image, HttpServletRequest request,HttpServletResponse response, HttpSession httpSession,@RequestParam("file") MultipartFile file) {
// …… 省略掉与flask交互无关的代码
                request.setAttribute("imagePath", image.getImagePath());
                
                //访问flask接口,把需要预测的图片的地址传过去 
                                //这里发送的请求是GET请求吗? 怎么改成POST请求呢?
                response.sendRedirect("http://127.0.0.1:5000/workspace/result?pathP=" + 
                                "D:/STS/workspace-spring-tool-suite-4-4.1.0.RELEASE/BoneAge/src/main/resources/static/img/" + 
                            fileName + "&imagePathP=" + image.getImagePath());

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        return "user/result"; //预测后跳转到result.html
    }
@app.route("/workspace/result",methods=["GET","POST"])   
def index():
    if request.args.get("path", "") != "":
        month = float(boneage.predict(request.args['path'])) #当用户是医生时
        imagepath = request.args['imagePath']
        return redirect(
            'http://localhost:8080/doctor/result_dr?month=' + str(month) + '&imagePath=' + str(
                imagepath))
    else:
        monthP = float(boneage.predict(request.args['pathP']))  #当用户是患者时
        imagepathP = request.args['imagePathP']
        print(request.method)
        print(monthP)
        return redirect(
            'http://localhost:8080/user/result?month=' + str(monthP) + '&imagePathP=' + str(
                imagepathP))   

  • 写回答

5条回答 默认 最新

  • 普通网友 2023-02-12 21:28
    关注

    不知道你解决了没有?如果没有解决,我们可以聊聊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 2月22日
  • 已采纳回答 2月14日
  • 赞助了问题酬金15元 2月12日
  • 创建了问题 2月12日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装