使用Python请求将文件上载到springrest服务器

2024-10-01 02:39:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用python请求将一个文件上传到我的java/scalaspringrest服务器。我得到的答复如下:

{"timestamp":1454331913056,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required MultipartFile parameter 'image' is not present","path":"/parking_api/images"}

我的服务器代码:

^{pr2}$

我的客户代码:

requests.post(parking_webservice_address, files={"image": ("image", open(event.pathname, "rb"), "image/jpeg")})

我试过:

  • 在python代码中将files参数设置为{"image":open(...)}而不是元组
  • 在python代码中,使用open(...).read()files参数将加载的图像数据传递到内存
  • 在服务器中设置CommonsMultipartResolver,如下所示

    @Bean(name="multipartResolver") public CommonsMultipartResolver multipartResolver(){ return new CommonsMultipartResolver(); }

  • 手动设置多部分头,但it服务器期望随着情况的恶化而丢失边界

这些选择对我都没用。我错过了什么?在


Tags: 文件代码image服务器webstatuserrorfiles