问题描述
我在java中的post方法遇到问题:
@POST
@Path("/test")
@Produces("application/xml")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String testing(MultipartFormDataInput input)
{
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
String name = this.getFormValue(uploadForm, "name");
List<InputPart> inputParts = uploadForm.get("file");
...
}
清单显示了POST方法的开始,该方法使用一个文件和一些其他参数,例如“ name”。 只要我提供一些参数和文件作为输入,一切都可以正常工作。 如果没有提供文件,我也想用相同的方法提供一些输出。 但是在这种情况下,我总是会收到此错误 :
java.io.IOException:RESTEASY007550:无法获取多部分的边界
如果我将请求的内容标头手动设置为multipart / form-data,甚至是这种情况。 有什么解决方案可以通过一种POST方法处理这两种用例(参数+文件和仅参数)?
谢谢您的帮助!
python中的客户端代码:
payload = {"name": "test"}
file = {"file": open("test.zip", "rb")}
url = "http://localhost:8080/test_war/test/test"
r = requests.post(url, data=payload, files=file)
print r.text
print r.status_code
1楼
问题出在您正在使用的客户端库中: :
该行假设,如果您传递文件,则它是一个多部分请求,没有文件,它将执行其他操作。
添加具有不同参数名称的假文件应满足您的服务器端代码:
file_name ="test.zip"
if file_name:
files = {"file": open("test.zip", "rb")}
else:
files = {"dummy_file": "nothing"}
url = "http://localhost:8080/test_war/test/test"
r = requests.post(url, data={"name": "test"}, files=files)
print r.text
print r.status_code