项目的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
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))