英文:
Translation of python API request to Java not working
问题
以下是翻译好的内容:
我正在尝试使用Java复制一个非常简单的Python函数,该函数调用远程服务。我没有服务文档。以下是Python代码:
def sendImage(addr, image_path):
image = open(image_path, 'rb').read()
files = { 'image': ('Image.jpg', image, 'image/jpg') }
response = requests.post(addr, files = files)
print(response.text)
但我似乎无法使其工作。以下是我目前的Java代码:
try {
URL url = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "image/jpeg");
connection.setConnectTimeout(Constants.TIMEOUT);
connection.setReadTimeout(Constants.TIMEOUT);
try (OutputStream out = connection.getOutputStream()) {
IOUtils.copy(image, out);
}
try (InputStream in = connection.getInputStream()) {
Map<String, String> originalResponse = mapper.readValue(in, TypeFactory.defaultInstance().constructMapType(HashMap.class, String.class, String.class));
}
}
catch (Exception e) {
e.printStackTrace();
}
我使用相同的addr和相同的图像调用了这两个函数,但当我运行Java代码时,应用程序会在以下几行代码中停顿几秒钟:
try (InputStream in = connection.getInputStream()) {
然后我得到一个"400: Bad Request"的响应。
为什么会出现问题,我该如何使其工作?提前谢谢。
英文:
I'm trying to replicate, using Java, a very simple Python function that calls a remote service. I don't have the service documentation. Here's the Python code:
def sendImage(addr, image_path):
image = open(image_path, 'rb').read()
files = { 'image': ('Image.jpg', image, 'image/jpg') }
response = requests.post(addr, files = files)
print(response.text)
But I can't seem to get this to work. Here's my Java code so far:
try {
URL url = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "image/jpeg");
connection.setConnectTimeout(Constants.TIMEOUT);
connection.setReadTimeout(Constants.TIMEOUT);
try (OutputStream out = connection.getOutputStream()) {
IOUtils.copy(image, out);
}
try (InputStream in = connection.getInputStream()) {
Map<String, String> originalResponse = mapper.readValue(in, TypeFactory.defaultInstance().constructMapType(HashMap.class, String.class, String.class));
}
}
catch (Exception e) {
e.printStackTrace();
}
I'm calling both functions with the same addr and using the same image, but when I run my Java code, the app hangs in this line for a few seconds
try (InputStream in = connection.getInputStream()) {
and then I get a "400: Bad Request" response.
Why is this not working, and how can I make it work? Thanks in advance
答案1
得分: 1
response = requests.post(addr, files = files)
是使用多部分编码进行文件上传(请参阅api参考)。在你的Java代码中,你只需将文件内容写入请求的主体。
如果你想编写代码来正确处理多部分请求,请查看这个RFC,了解请求主体中必须包含的内容。
我建议你使用一些HTTP客户端。这里是使用okhttp上传的示例。
英文:
response = requests.post(addr, files = files)
is file upload with multipart encoding (see api reference) and in your java code you just write file content as body of request.
If you want to write code to properly handle multipart request, then check out this RFC, what you must include in request body.
My suggestion would be to use some http client. Here is example of upload with okhttp.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论